使用 Javascript 将邮政编码传递到maps.google.com 来获取纬度经度
我的大型数据库中有邮政编码,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于那些找到这个答案的人来说,这是一个快速说明。 Daniel Vassallo 的答案使用了 Google Geocoding API V2,该 API 现已弃用。新的 v3 API 使用如下请求格式:
邮政编码查找的示例,以 JSON 格式返回数据是:
这将返回一个 JSON 数组,其中包含结果中的纬度和经度 -> 几何 -> 位置 -> 位置。 lat 和 results->geometry->location->lng
响应示例:
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:
An example for a postcode lookup, returning the data in JSON format is:
This returns a JSON array that includes the lat and long in results->geometry->location->lat and results->geometry->location->lng
Example response:
The API spec is available here: https://developers.google.com/maps/documentation/geocoding/
您描述的过程的技术术语称为反向地理编码。 Google 提供
Google 地理编码网络服务新的可用 Google 地理编码链接,您可以在其中执行反向操作在服务器端进行地理编码,而不是在客户端使用 JavaScript 进行地理编码。例如,如果您在浏览器中尝试以下 URL,您将获得以 CSV 格式传递到
q
参数中的邮政编码的纬度和经度:这就是您能够反转的方式在 php 中对邮政编码进行地理编码,例如:
请注意 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 ServiceNew 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:This is how you would be able to reverse geocode your postcodes in php, for example:
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.
英国地形测量局已在知识共享许可上发布了邮政编码位置 (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
尽管 Google 地理编码 API 可以实现这一点如果我没记错的话,他们的服务条款禁止本地存储地理编码结果。
The Google Geocoding API does that, although if I remember correctly, their terms of service forbid local storage of the geocoding results.
我知道这是一个老问题,但只是在这里补充一下我如何设法实现同样的事情(在 PHP 中,但应该相当简单):
我有一个包含数千个不同格式的邮政编码的数据库。我用这个函数和批量更新统一清理了它们中的每一个:
现在所有邮政编码的格式都统一了,我下载并导入了地形测量局的代码点数据集(免费) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html
我将他们的所有 CSV 导入到我的数据库中的一个单独的
codepoint
表中。我从每个 CSV 中导入了邮政编码以及东距和北距值。我对数据库中的所有地形测量数据再次批量运行了
clean_postcode()
函数。大约 50% 的邮政编码有空格,50% 没有 - 此后它们被统一设置。我对
codepoint
表中的每个邮政编码运行了以下 PHP 脚本,并将返回的纬度和经度值保存到该表中:http://bramp.net/blog/os-easting-northing-to-lat-long全部完成!现在,您可以根据格式正确的邮政编码匹配并提取纬度/经度值。
进一步阅读: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):
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:
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
codepoint
table. 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
codepoint
table 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.
Further reading: http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
此处查看 ALgolia 地点。超快、开源和 MIT
Check out ALgolia places here. Super fast, Open source and MIT