通过使用Javascript将邮政编码传递到maps.google.com来获取纬度经度

Nir*_*mal 10 javascript asp.net google-maps geolocation

我在我的大型数据库中有邮政编码,其中包含SL5 9JH,LU1 3TQ等值.

现在,当我将上述邮政编码粘贴到maps.google.com时,它指向一个完美的位置..

我的要求是我想将邮政编码传递给maps.google.com,它应该返回该指向位置的相关纬度和经度,我想存储在我的数据库中.

所以,最有可能应该有一些javascript ...如果有人有另一个想法,请提供它..

提前致谢...

Cha*_*esA 14

找到这个答案的人快速注意到.Daniel Vassallo的回答使用了Google Geocoding API V2,现已弃用.新的v3 API使用如下的请求格式:

http://maps.googleapis.com/maps/api/geocode/output?parameters
Run Code Online (Sandbox Code Playgroud)

邮政编码查找的一个示例,以JSON格式返回数据:

http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false
Run Code Online (Sandbox Code Playgroud)

这将返回一个JSON数组,其中包括lat和long in results-> geometry-> location-> lat和results-> geometry-> location-> lng

响应示例:

{
 "results" : [
  {
     "address_components" : [
        {
           "long_name" : "SL5 9JH",
           "short_name" : "SL5 9JH",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Windsor and Maidenhead",
           "short_name" : "Windsor and Maidenhead",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "Ascot",
           "short_name" : "Ascot",
           "types" : [ "postal_town" ]
        }
     ],
     "formatted_address" : "Ascot, Windsor and Maidenhead SL5 9JH, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 51.39655490000001,
              "lng" : -0.66024660
           },
           "southwest" : {
              "lat" : 51.39457330,
              "lng" : -0.6624574999999999
           }
        },
        "location" : {
           "lat" : 51.39539040,
           "lng" : -0.66096740
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 51.39691308029150,
              "lng" : -0.6600030697084980
           },
           "southwest" : {
              "lat" : 51.39421511970851,
              "lng" : -0.6627010302915021
           }
        }
     },
     "types" : [ "postal_code" ]
  }
],
"status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)

API规范可在此处获取:https://developers.google.com/maps/documentation/geocoding/


Dan*_*llo 10

您描述的过程的技术术语称为反向地理编码.Google提供Google地理编码网络服务 新的Google地理编码链接,您可以在服务器端执行反向地理编码,而不是在客户端使用JavaScript.

例如,如果您在浏览器中尝试以下网址,则会q以CSV格式返回参数中传递的邮政编码的纬度和经度:

http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false

http://maps.google.com/maps/geo?q=LU13TQ,+UK&output=csv&sensor=false
Run Code Online (Sandbox Code Playgroud)

这就是你可以在php中反转地理编码你的邮政编码的方式,例如:

$url = 'http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false';

$data = @file_get_contents($url);

$result = explode(",", $data);

echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude
Run Code Online (Sandbox Code Playgroud)

请注意,正如Pekka在另一个答案中建议的那样,Google Maps API使用条款似乎禁止存储结果,除非商店充当Google地图中使用的数据的缓存.您可能希望与Google取得联系,并在Google Maps API Premier上查询,以便为您的地理编码要求提供更灵活的使用条款.


Row*_*haw 9

军械测量局已在Creative Commons许可证(CC-BY v3,IIRC)上发布了邮政编码位置.使用它可以减少麻烦(并且在法律上更清晰).

甚至有一个由mySociety镜像的WGS84(又名GPS)坐标的版本