0

console.log提供了一個未定義的座標值。如果我們在if函數內部使用console.log coord,它會給出這個值,但是好像這些值不能從地理編碼函數中傳出。地理編碼範圍Google Maps API

有誰知道地址解析的範圍是什麼,我如何能夠改變從地理編碼函數獲取值嗎?

THANKS

var coord; 

    function change_coord(location) { 
    var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({'address': location}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       //real_lat and real_lng are int values 
       var real_lat = results[0].geometry.location.lat(); 
       var real_lng = results[0].geometry.location.lng(); 
       coord = {lat: real_lat, lng: real_lng}; 
      } 
     }); 
    console.log(coord); //undefined 
    } 
+2

我沒有使用過的GeoCoder但看起來像你需要把執行console.log(座標)if塊內,因爲geocoder.geocode看起來像某種異步調用的到後端,它需要時間來給出結果和之前的收益你正在打印座標 – jsmtslch

回答

2

嘗試用地理編碼器回調函數內的console.log。 在您的代碼中,console.log會在您收到地理編碼響應之前執行。

相關問題