【发布时间】:2017-03-08 21:48:30
【问题描述】:
console.log 给出了一个未定义的坐标值。如果我们在 if 函数中进行 console.log 坐标,它会给出值,但似乎这些值不能来自 geocode 函数。
有谁知道地理编码的范围是什么以及如何更改以从地理编码函数中获取值?
谢谢
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
}
【问题讨论】:
-
我没有使用过 GeoCoder,但看起来你需要将 console.log(coord) 放在 if 块中,因为 geocoder.geocode 看起来像是对后端的某种异步调用,需要时间来提供结果,在返回之前,您正在打印坐标
标签: javascript google-maps google-maps-api-3 google-maps-api-2 google-geocoder