【发布时间】:2015-03-24 12:01:33
【问题描述】:
希望你能帮上忙。我做了一个函数,它接收一个 lnglat 点对象并只返回城镇。我可以让它在 console.log 中打印正确的城镇,但它不会从函数返回数据。
我知道这将是一个基本错误,但有人可以查看代码并告诉我。
提前致谢。
function getTownFromPoint(point){
var geocoder ;
var geocoder = new google.maps.Geocoder();
var townset = false;
mylocation = "";
geocoder.geocode({latLng: point}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var components=results[0].address_components;
for (var component=0;component<(components.length);component++){
if(components[component].types[0]=="country" & !townset){
mylocation = components[component].long_name;
}
if(components[component].types[0]=="postal_code" & !townset){
mylocation = components[component].long_name;
}
if(components[component].types[0]=="locality"){
mylocation = components[component].long_name;
townset = true;
console.log(mylocation);
}
}
}
}
});
return(mylocation);
}
【问题讨论】:
标签: javascript maps geocode