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会在您收到地理编码响应之前执行。

相关问题