使用 Google Maps Javascript API V3 反向地理编码检索邮政编码
每当谷歌地图视口中心发生变化时,我都会尝试使用邮政编码向我的数据库提交查询。我知道这可以通过反向地理编码来完成,例如:
google.maps.event.addListener(map, 'center_changed', function(){
newCenter();
});
...
function newCenter(){
var newc = map.getCenter();
geocoder.geocode({'latLng': newc}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var newzip = results[0].address_components['postal_code'];
}
});
};
当然,这个代码实际上不起作用。所以我想知道我需要如何更改它才能从结果数组中提取邮政编码。 谢谢
I'm trying to submit a query using the postal code to my DB whenever the googlemaps viewport center changes. I know that this can be done with reverse geocoding with something like:
google.maps.event.addListener(map, 'center_changed', function(){
newCenter();
});
...
function newCenter(){
var newc = map.getCenter();
geocoder.geocode({'latLng': newc}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var newzip = results[0].address_components['postal_code'];
}
});
};
Of course, this code doesn't actually work. So I was wondering how I would need to change this in order to extract the postal code from the results array.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
到目前为止我已经意识到,在大多数情况中,邮政编码
始终是每个返回地址中的最后一个值,因此,如果您想检索第一个邮政编码(这是我的情况),您可以使用以下方法:What I've realized so far is that in most cases the ZIPCODE is
alwaysthe last value inside each returned address, so, if you want to retrieve the very first zipcode (this is my case), you can use the following approach:您可以使用 underscore.js 库轻松完成此操作: http://documentcloud.github.com/underscore /#查找
You can do this pretty easily using the underscore.js libraray: http://documentcloud.github.com/underscore/#find
使用 JQuery?
短名称或长名称将在上面工作
“searchPostalCode”变量将包含邮政(zip?)代码 IF
并且仅当您从 Google Maps API 获取地图时。
有时,您的查询不会得到“邮政编码”作为回报。
Using JQuery?
short_name or long_name will work above
the "searchPostalCode" var will contain the postal (zip?) code IF
and only IF you get one from the Google Maps API.
Sometimes you DO NOT get a "postal_code" in return for your query.
好吧,所以我明白了。该解决方案比我想要的要丑一些,而且我可能不需要最后一个 for 循环,但这里是其他需要从 address_components[] 中提取垃圾的人的代码。这是在地理编码器回调函数内部
Alright, so I got it. The solution is a little uglier than I'd like, and I probably don't need the last for loop, but here's the code for anyone else who needs to extract crap from address_components[]. This is inside the geocoder callback function
您还可以使用 JavaScript
.find
方法,类似于下划线_.find
方法,但它是本机的,不需要额外的依赖。You can also use JavaScript
.find
method which is similar to underscore_.find
method but it is native and require no extra dependency.这只需要两个 for 循环。一旦我们发现第一个“类型”是“邮政编码”,“结果”数组就会更新。
然后它用新找到的数组集更新原始数组并再次循环。
This takes only two for loops. The "results" array gets updated once we found the first "type" to be "postal_code".
It then updates the original array with the newly found array set and loops again.
您还可以使用此代码,此功能将有助于在单击按钮或 onblur 或 keyup 或 keydown 时获取 zip。
只需将地址传递给该函数即可。
使用 google api 并删除有效密钥和传感器选项,因为现在不需要。
You can also use this code, this function will help to get zip on button click or onblur or keyup or keydown.
Just pass the address to this function.
use google api with valid key and sensor option removed as it doesn't required now.
我使用此代码来获取“邮政编码”和“地点”,但您可以使用它来获取任何其他字段,只需更改类型的值:
JAVASCRIPT
I use this code to get "Postal code" and "locality", but you can use it to get any other field just changing the value of type:
JAVASCRIPT
我认为与其依赖索引,不如检查组件内部的地址类型键。我通过使用开关盒解决了这个问题。
I think rather than depending on the index it better checks address type key inside the component. I solved this issue by using a switch case.
这对我来说似乎非常有效,这是与新的 Google Maps API V3 一起使用的。如果这对任何人有帮助,请写评论,我正在写我的脚本......所以它可能会改变。
This seems to work very well for me, this is with the new Google Maps API V3. If this helps anyone, write a comment, i'm writing my script as we speak... so it might change.
使用 JSONPath,只需一行代码即可轻松完成:
Using JSONPath, it's easily done with one line of code:
在 PHP 中我使用这段代码。几乎在所有条件下它都有效。
In PHP I use this code. Almost in every conditions it works.
罗曼·M.——谢谢!如果您只需要在 Google 返回的第一个结果中查找邮政编码,则只需执行 2 个循环:
Romaine M. — thanks! If you just need to find the postal code in the first returned result from Google, you can do just 2 loops:
总而言之,这是一个很大的努力。至少使用 v2 API,我可以这样检索这些详细信息:
必须有一种更优雅的方法来检索各个地址组件,而无需经历您刚刚经历的循环柔术。
In a word, that's a lot of effort. At least with the v2 API, I could retrieve those details thusly:
There has got to be a more elegant way to retrieve individual address_components without going through the looping jujitsu you just went through.
这个简单的代码对我有用
This simple code works for me
使用 Jquery
Using Jquery
看来现在最好从restful API获取它,只需尝试:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_KEY_HERE
使用 AJAX GET 调用效果非常好!
像这样的东西:
It seems that nowadays it's better to get it from the restful API, simply try:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_KEY_HERE
Using an AJAX GET call works perfect!
Something like:
据我所知, zip 是最后一个或最后一个之前的一个。
这就是为什么这是我的解决方案
As I got it zip is the last or the one that before last.
That why this is my solution
我认为这是最准确的解决方案:
i think this is the most accurate solution:
只需在所有类型中搜索
postal_code
,找到即可返回。Just search for
postal_code
in all types, and return when found.