-1
從單擊按鈕調用Geocoder方法時不會返回值。我需要將結果存儲在全局變量「source_lat」和「source_long」中。似乎我的按鈕點擊在地理編碼器可能返回值之前結束。Geocoder方法不會返回值
function GeocoderStart(str) {
geocoder = new google.maps.Geocoder();
geocoder_request = { 'address': str };
geocoder.geocode(geocoder_request, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var country = extractFromAdress(results[0].address_components, "country");
if ((country != "India") & (country != "")) {
//alert("Error", "The address you entered refers to <b>" + results[0].formatted_address + "</b>. Please select only addresses in India using the autocomplete lookup.");
alert("Please select a valid address");
return false;
} else {
source_lat = results[0].geometry.location.lat();
source_long = results[0].geometry.location.lng();
}
} else {
alert("Error", "Unable to lookup address for the following reason: " + status);
return false;
}
});
}
//從地址功能
function extractFromAdress(components, type) { // taken from http://stackoverflow.com/questions/8313876/more-efficient-way-to-extract-address-components
for (var i = 0; i < components.length; i++)
for (var j = 0; j < components[i].types.length; j++)
if (components[i].types[j] == type) return components[i].long_name;
return "";
}
是不是異步? –
你會得到什麼? _請選擇一個有效的地址_或另一個? – putvande
@ A.Wolff是的。 –