我是JavaScript新手。我正在嘗試使用Google api的Gmap v3來實現反向地理編碼。我讀過很多教程並編寫了一個簡單的代碼。問題是傳遞給geocoder.geocode()的匿名函數有時會起作用,但有時卻不行。謝謝你的幫助!。不調用地理編碼反向回調函數
function geoCode(latStr,lngStr){
var lat = parseFloat(latStr);
var lng = parseFloat(lngStr);
var latlng = new google.maps.LatLng(lat, lng);
codeLatLng(latlng,function(addr){
alert(addr); // sometimes message appears.
});
}
function codeLatLng(latlng,callback) {
if (geocoder) {
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
callback(results[1].formatted_address);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
}
你是什麼意思「有時工作,但有時不」?發生這種情況時,你在哪裏使用它?你會得到其他警報嗎? – geocodezip
我的意思是,我沒有得到任何其他警報......有時不叫它的匿名函數。 – AntarticMonkey