问题描述
我试图在Google地图视口中心发生更改时使用邮政编码将数据提交到我的数据库。我知道这可以通过反向地理编码来完成,例如:
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
推荐答案
好吧,我知道了。该解决方案比我想要的有点丑,我可能不需要最后一个for循环,但是这里是需要从address_components []中提取垃圾的其他人的代码。这是地理编码器回调函数中的内容(b; $ p
$ b
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
for(i; i < results.length; i++){
for(var j=0;j < results[i].address_components.length; j++){
for(var k=0; k < results[i].address_components[j].types.length; k++){
if(results[i].address_components[j].types[k] == "postal_code"){
zipcode = results[i].address_components[j].short_name;
}
}
}
}
这篇关于使用Google Maps JavaScript API V3反向地理编码检索邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!