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

发布于 2024-08-28 04:24:20 字数 258 浏览 4 评论 0原文

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

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

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

所以,很可能应该有一些 javascript...如果有人对此有其他想法,请提供它..

提前致谢...

I have Postcode in my large database, which contains values like SL5 9JH, LU1 3TQ etc.

Now when I am pasting above postcode to maps.google.com it's pointing to a perfect location..

My requirement is like I want to pass post codes to maps.google.com and it should return a related latitude and longitude of that pointed location, that I want to store in my database.

So, most probably there should be some javascript for that... If anybody have another idea regarding that please provide it..

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悍妇囚夫 2024-09-04 04:24:20

对于那些找到这个答案的人来说,这是一个快速说明。 Daniel Vassallo 的答案使用了 Google Geocoding API V2,该 API 现已弃用。新的 v3 API 使用如下请求格式:

http://maps.googleapis.com/maps/api/geocode/output?parameters

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

http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false

这将返回一个 JSON 数组,其中包含结果中的纬度和经度 -> 几何 -> 位置 -> 位置。 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"
}

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

A quick note for those finding this SO answer. The answer by Daniel Vassallo uses the Google Geocoding API V2 which has now been deprecated. The new v3 API uses a request format like this:

http://maps.googleapis.com/maps/api/geocode/output?parameters

An example for a postcode lookup, returning the data in JSON format is:

http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false

This returns a JSON array that includes the lat and long in results->geometry->location->lat and results->geometry->location->lng

Example response:

{
 "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"
}

The API spec is available here: https://developers.google.com/maps/documentation/geocoding/

家住魔仙堡 2024-09-04 04:24:20

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

例如,如果您在浏览器中尝试以下 URL,您将获得以 CSV 格式传递到 q 参数中的邮政编码的纬度和经度:

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

这就是您能够反转的方式在 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

请注意 Pekka 在另一个答案中建议,Google Maps API 使用条款似乎禁止存储结果< /a>,除非存储充当 Google 地图中使用的数据的缓存。您可能需要与 Google 联系并查询 Google Maps API Premier 拥有更灵活的使用条款来满足您的地理编码要求。

The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web Service New working Google Geocoding Link, where you can do reverse geocoding on the server side, instead of in JavaScript on the client-side.

For example, if you try the following URLs in your browser, you would get back the latitude and longitude of the postcode passed in the q parameter, in CSV format:

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

This is how you would be able to reverse geocode your postcodes in php, for example:

$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

Note that as Pekka suggested in another answer, the Google Maps API Terms of Use seem to prohibit the storage of the results, unless the store acts as a cache for data that will used in Google Maps. You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements.

淡墨 2024-09-04 04:24:20

英国地形测量局已在知识共享许可上发布了邮政编码位置 (CC-BY v3,IIRC)。改用它会少很多麻烦(而且在法律上也更清晰)。

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

The Ordnance Survey have released the postcode locations on a Creative Commons licence (CC-BY v3, IIRC). It would be a lot less hassle (and a lot clearer legally) to use that instead.

There's even a version with WGS84 (a.k.a. GPS) coordinates mirrored by mySociety

秋心╮凉 2024-09-04 04:24:20

尽管 Google 地理编码 API 可以实现这一点如果我没记错的话,他们的服务条款禁止本地存储地理编码结果。

The Google Geocoding API does that, although if I remember correctly, their terms of service forbid local storage of the geocoding results.

成熟的代价 2024-09-04 04:24:20

我知道这是一个老问题,但只是在这里补充一下我如何设法实现同样的事情(在 PHP 中,但应该相当简单):

  1. 我有一个包含数千个不同格式的邮政编码的数据库。我用这个函数和批量更新统一清理了它们中的每一个:

    函数 clean_postcode($postcode)
    {
        $out = preg_replace("/[^a-zA-Z0-9]/", '',strtoupper($postcode));
    
        if(strlen($out)>3) 
        {
            $out = substr($out, 0, (strlen($out) -3)).' '.substr($out, -3);
        }
    
        返回 $out;
    }
    
  2. 现在所有邮政编码的格式都统一了,我下载并导入了地形测量局的代码点数据集(免费) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html

  3. 我将他们的所有 CSV 导入到我的数据库中的一个单独的 codepoint 表中。我从每个 CSV 中导入了邮政编码以及东距和北距值。

  4. 我对数据库中的所有地形测量数据再次批量运行了 clean_postcode() 函数。大约 50% 的邮政编码有空格,50% 没有 - 此后它们被统一设置。

  5. 我对 codepoint 表中的每个邮政编码运行了以下 PHP 脚本,并将返回的纬度和经度值保存到该表中:http://bramp.net/blog/os-easting-northing-to-lat-long

  6. 全部完成!现在,您可以根据格式正确的邮政编码匹配并提取纬度/经度值。

进一步阅读:http://www.deepbluesky.com /blog/-/converting-os-coodinates-into-longitude-latitude_7/

I know this is an old question, but just chipping in here with how I managed to achieve the same thing (in PHP, but should be fairly simple):

  1. I had a database of thousands of differently formatted postcodes. I cleaned each and every one of them up, uniformly, with this function and batch updates:

    function clean_postcode($postcode)
    {
        $out = preg_replace("/[^a-zA-Z0-9]/", '',strtoupper($postcode));
    
        if(strlen($out)>3) 
        {
            $out = substr($out, 0, (strlen($out) -3)).' '.substr($out, -3);
        }
    
        return $out;
    }
    
  2. Now that all postcodes are formatted uniformly, I downloaded and imported the Ordnance Survey's Code-Point data set (free) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html

  3. I imported all their CSVs into my database in a separate codepoint table. From each CSV I imported the postcode, and the Eastings and Northings values.

  4. I ran the clean_postcode() function again in batch on all the Ordnance Survey data in my DB. About 50% of the postcodes have spaces in, and 50% don't - after this they were uniformly set.

  5. I ran the following PHP script on each and every postcode in the codepoint table and saved the Latitude and Longitude values returned into this table: http://bramp.net/blog/os-easting-northing-to-lat-long

  6. All done! You can now match up and pull a Lat/Lon value based on well-formatted postcodes.

Further reading: http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/

逆光下的微笑 2024-09-04 04:24:20

此处查看 ALgolia 地点。超快、开源和 MIT

Check out ALgolia places here. Super fast, Open source and MIT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文