2011-11-17 26 views
1

我想要做的是我需要從存儲的用戶位置創建標記。所以我需要經緯度。我在函數get_current_user_lat_lon中得到了lat & lon。現在我想通過對象LAT & LON同時返回,但我的回報是不工作....從javascript函數返回多個值給谷歌地圖V3中的錯誤

function initialize(){ 
    var var_get_current_user_lat_lon = get_current_user_lat_lon(); 
    var latitutde = var_get_current_user_lat_lon.latitutde; 
    alert(latitutde);// not even alert 
    var longitutde = var_get_current_user_lat_lon.longitutde; 
    alert(longitutde); // not even alert 
}   

function get_current_user_lat_lon(){ 
    var address = $("#hidden_current_location").val();//Working fine 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var variable = results[0].geometry.location; 
      var lat = variable.lat(); 
      var lon = variable.lng(); 
      alert(lat); //Working fine 
      alert(lon); //Working fine 
      //return not working here 
     } 
     //return not working here 
    }); 
    return {latitutde: lat , longitutde : lon};// giving error lat.lon is not defined means return not working here 
} 

回答

1

你的「緯度」和「經度」變量聲明從回調「的地址解析()」 。然而,即使你在外面宣佈他們,整體的想法也是行不通的。 「地理編碼()」功能是異步;這就是首先有回調函數的原因。

而不是構建你的代碼,這樣你的「get_current_user_lat_lon()」函數返回值,按照谷歌架構的領導,使自己的功能需要一個回調:

function get_current_user_lat_lon(callback){ 
    var address = $("#hidden_current_location").val();//Working fine 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     var variable = results[0].geometry.location; 
     var lat = variable.lat(); 
     var lon = variable.lng(); 
     callback({latitude: lat, longitude: lon}); 
     } 

    }); 

} 
+0

所以做什麼現在??我應該去做file_get_contents(谷歌地圖等等)docodeing n等...... –

+0

是的 - 你必須做的事情,你需要做的事情之後,從*回調函數內*返回。 – Pointy

+0

請你可以編輯我的代碼請稍微....所以我得到一個線索 –

相關問題