2
我正在使用谷歌地圖v3 geocoder地址解析地址,然後通過2個座標點從jQuery文件到PHP文件使用getJSON
。函數返回undefined在地理編碼器
問題:但是,我注意到做地理編碼功能的函數一直返回一個未定義的值!因此PHP文件接收一個未定義的變量。我哪裏做錯了?
jQuery代碼
var search_latlng = geocodeAddress(search_location);
console.log(search_latlng);
$.getJSON('/main/get_places', {search_location: search_latlng}, function(json){
$("#result_listing").html('');
.
.
.
地理編碼器JS功能
function geocodeAddress(address) {
var latlng = new Array(2);
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latlng[0] = results[0].geometry.location.lat();
latlng[1] = results[0].geometry.location.lng();
return latlng;
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
}
感謝您的回覆!所以'callback'是其他地方定義的一個普通函數,我將它傳遞給'geocoderAddress()'? 'callback()'函數可以使用'latlng'變量並將其返回給另一個變量'search_latlng'以傳遞給'getJSON'函數? – Nyxynyx
本質上,您所做的與Google API處理核心異步事務(滿足地理編碼查詢的底層HTTP請求)完全相同。您將一個函數傳遞給Google API,因此* your * API將被寫入,以便在您從別處調用它時傳遞一個處理函數。 – Pointy
嗨Pointy,我的javascript不是很好,所以我可以'理解你。你能在你發佈的代碼中編寫一個示例回調函數代碼嗎?我嘗試了谷歌搜索,但似乎不是我明白你告訴我的回調函數。 – Nyxynyx