可能重复:
未定义不是函数,谷歌地理定位
我正在尝试获取位置的谷歌反向地理编码,它返回我未定义但在 console.log 它显示地址
这是获取值的函数
function getLatLng(latlng, callback) {
var codedAddress;
geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
codedAddress = results[1].formatted_address;
console.log("codedAddress 1 = "+codedAddress);
} else {
alert("There was a problem with the map");
}
console.log("codedAddress 2 = "+codedAddress);
});
callback(codedAddress);
}
这是我的初始化代码
function initialize() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(5.386, 100.245),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var location_coords = [
<?php echo $output; ?>
];
var location_status = [
<?php echo $status; ?>
];
var location_users = [
<?php echo $users; ?>
];
var location_date = [
<?php echo $date; ?>
];
for(i=0;i<location_coords.length;i++) {
var traffic_options = {
strokeColor: '#A52A2A',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: getColor(location_status[i]),
fillOpacity: 0.5,
map: map,
center: location_coords[i],
radius: 300
};
cityCircle = new google.maps.Circle(traffic_options);
barr[i] = new Object;
var marker = new google.maps.Marker({
position: location_coords[i],
map: map,
});
barr[i].marker = marker;
barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" +
"Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; }) +
"<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>";
barr[i].infoWindow = new google.maps.InfoWindow({
content: barr[i].html
});
barr[i].listener = makeClosure(i, barr[i].marker);
}
}
问题出在这个语句中
"Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; })
我如何在此处获取值而不是“未定义”
谢谢