我希望使用lang值来查找地址,因为我使用的是新的google.maps.Geocoder().geocode(),下面是该方法的实现
function checkAddress(){
var address="";
var amstr = new google.maps.LatLng(37.556179,-122.288219);
address = getAddress(amstr);
alert(address);
}
function getAddress(latLng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
return results[0].formatted_address;
}
else {
return "No results";
}
}
else {
return "not working";
}
});
}但在运行以上代码后,结果是“未定义”。请告诉我我在哪里做错了。
谢谢
发布于 2012-12-13 15:25:51
geocoder.geocode()是异步的。它只在其余代码完成后的某个时间调用您的回调。
您需要在回调中返回值:
function getAddress(latLng, callback) {
...
geocoder.geocode(..., function(...) {
...
callback(someValue);
});
}https://stackoverflow.com/questions/13862645
复制相似问题