我正在嘗試創建一個利用Google Javascript V3的地理編碼功能並返回一個具有經度和緯度的數組的函數。出於某種原因,返回數組沒有使用該函數進行填充。謝謝你的幫助!如何使用Google Javascript v3 Geocoder返回經緯度數組?
代碼:
function getCoords(address) {
var latLng = [];
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latLng.push(results[0].geometry.location.lat());
latLng.push(results[0].geometry.location.lng());
return latLng;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
var test_arr;
test_arr = getLatLng('New York');
alert(test_arr[0] + ',' + test_arr[1]) // I'm getting a test_arr is undefined here.
異步API不同步。做回調工作。 – 2013-02-10 03:39:16