【发布时间】:2014-04-04 00:43:57
【问题描述】:
我有这段代码,用于在表单上填充纬度和经度的两个文本输入。我想知道是否有办法检索城市、街道和邮政编码,以便我也可以使用它们来填充表单?
google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
});
例子:
$zipcode.value = latLng.postal_code();
$street.value = latLng.street_address();
$city.value = latLng.city();
这是我的完整代码:
function selectState(state_id){
if(state_id!="-1"){
loadData('city',state_id);
var e = document.getElementById("state");
var stateloc = e.options[e.selectedIndex].value;
var address = state_id + stateloc;
var $latitude = document.getElementById('latitude');
var $longitude = document.getElementById('longitude');
var latitude = 73.00636021320537;
var longitude = -23.46687316894531;
var zoom = 10;
geocoder = new google.maps.Geocoder();
var LatLng = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: zoom,
center: LatLng,
panControl: false,
zoomControl: true,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: 'Drag Me!',
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
});
} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}else{$("#city_dropdown").html("<option value='-1'>Select city</option>");
}
}
【问题讨论】:
-
它被称为“反向地理编码”。您可以在 developers.google.com/maps/documentation/geocoding/… 找到所需的一切
-
你是说你编的函数?
$zipcode.value = latLng.postal_code();,这些都行不通。您需要调用反向地理编码器(正如@Floris 在他的评论中指出的那样),然后解析出您想要的字段。 -
他们正在为纬度和经度工作。我不是已经调用反向地理编码器了吗?
标签: javascript google-maps google-maps-api-3