[英]get zipcode from latitude and longitude in javascript
想要从该位置的经度和纬度显示该位置的邮政编码。 下面是我的代码。
function markerLocation() {
var currentLocation = marker.getPosition();
var geocoder = new google.maps.Geocoder();
var location = new google.maps.LatLng(currentLocation.lat(), currentLocation.lng());
geocoder.geocode({ 'latLng': location , function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var add = results[0].address_components[1][1];
document.getElementById('lat').value = add;
}
});
在这里,我想显示纬度的邮政编码。 但是当我使用address_components时,它显示为undefined。 请帮助。
尝试这个:
<input id="postal_code"/>
<input id="street"/>
...
脚本
if (status == google.maps.GeocoderStatus.OK) {
for(var i in results[0].address_components) {
// skip items with no types
if(typeof results[0].address_components[i].types == 'undefined') {
continue;
}
if(results[0].address_components[i].types.indexOf('postal_code') > -1) {
document.getElementById('postal_code').value = results[0].address_components[i].long_name;
}
if(results[0].address_components[i].types.indexOf('street') > -1) {
document.getElementById('street').value = results[0].address_components[i].long_name;
}
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.