通过使用 Javascript 将邮政编码传递到 maps.google.com 来获取纬度经度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2621844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fetch Latitude Longitude by passing postcodes to maps.google.com using Javascript
提问by Nirmal
I have Postcode in my large database, which contains values like SL5 9JH, LU1 3TQetc.
我的大型数据库中有邮政编码,其中包含SL5 9JH、LU1 3TQ 等值。
Now when I am pasting above postcode to maps.google.com it's pointing to a perfect location..
现在,当我将上面的邮政编码粘贴到 maps.google.com 时,它指向一个完美的位置..
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.
我的要求就像我想将邮政编码传递给 maps.google.com,它应该返回该指向位置的相关纬度和经度,我想将其存储在我的数据库中。
So, most probably there should be some javascript for that... If anybody have another idea regarding that please provide it..
所以,很可能应该有一些 javascript ......如果有人对此有其他想法,请提供它..
Thanks in advance...
提前致谢...
采纳答案by Daniel Vassallo
The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web ServiceNew working Google Geocoding Link, where you can do reverse geocoding on the server side, instead of in JavaScript on the client-side.
您描述的过程的技术术语称为反向地理编码。Google 提供了Google Geocoding Web Service新的工作 Google Geocoding Link,您可以在其中在服务器端进行反向地理编码,而不是在客户端使用 JavaScript。
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 qparameter, in CSV format:
例如,如果您在浏览器中尝试以下 URL,您将返回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
This is how you would be able to reverse geocode your postcodes in php, for example:
这是您如何能够在 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
Note that as Pekka suggested in another answer, the Google Maps API Terms of Useseem 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 Premierto have more flexible terms of use for your geocoding requirements.
请注意,正如Pekka 在另一个答案中所建议的那样,Google Maps API 使用条款似乎禁止存储结果,除非商店充当将在 Google Maps 中使用的数据的缓存。您可能希望与 Google 取得联系并咨询Google Maps API Premier,以便为您的地理编码要求提供更灵活的使用条款。
回答by CharlesA
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:
对于那些找到这个 SO 答案的人的快速说明。Daniel Vassallo 的答案使用了现已弃用的 Google Geocoding API V2。新的 v3 API 使用这样的请求格式:
http://maps.googleapis.com/maps/api/geocode/output?parameters
An example for a postcode lookup, returning the data in JSON format is:
邮政编码查找的示例,以 JSON 格式返回数据是:
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
这将返回一个 JSON 数组,其中包括 results->geometry->location->lat 和 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/
API 规范可在此处获得:https: //developers.google.com/maps/documentation/geocoding/
回答by Rowland Shaw
The Ordnance Survey have released the postcode locationson a Creative Commons licence (CC-BY v3, IIRC). It would be a lot less hassle (and a lot clearer legally) to use that instead.
军械调查已根据知识共享许可 (CC-BY v3, IIRC)发布了邮政编码位置。使用它会减少很多麻烦(并且在法律上更清晰)。
There's even a version with WGS84 (a.k.a. GPS) coordinates mirrored by mySociety
甚至还有一个带有由mySociety镜像的 WGS84(又名 GPS)坐标的版本
回答by Pekka
The Google Geocoding APIdoes that, although if I remember correctly, their terms of service forbid local storage of the geocoding results.
在谷歌地理编码API这样做,但如果我没记错的话,他们的服务条款禁止地址解析结果的本地存储。
回答by Hyman
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):
我知道这是一个老问题,但只是在这里介绍我如何设法实现相同的目标(在 PHP 中,但应该相当简单):
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; }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
I imported all their CSVs into my database in a separate
codepointtable. From each CSV I imported the postcode, and the Eastings and Northings values.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.I ran the following PHP script on each and every postcode in the
codepointtable and saved the Latitude and Longitude values returned into this table: http://bramp.net/blog/os-easting-northing-to-lat-longAll done! You can now match up and pull a Lat/Lon value based on well-formatted postcodes.
我有一个包含数千个不同格式的邮政编码的数据库。我用这个函数和批量更新统一地清理了每一个:
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; }现在所有邮政编码都统一格式,我下载并导入了军械调查的代码点数据集(免费) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html
我将他们所有的 CSV 导入到我的数据库中的单独
codepoint表中。我从每个 CSV 文件中导入了邮政编码、Eastings 和 Northings 值。我
clean_postcode()对数据库中的所有军械测量数据再次批量运行该函数。大约 50% 的邮政编码中有空格,而 50% 没有 - 在此之后它们被统一设置。我在
codepoint表中的每个邮政编码上运行以下 PHP 脚本,并将返回的纬度和经度值保存到该表中:http: //bramp.net/blog/os-easting-northing-to-lat-long全部完成!您现在可以根据格式正确的邮政编码匹配并提取 Lat/Lon 值。
Further reading: http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
进一步阅读:http: //www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/

