所以,我有这段代码:如何从JavaScript中的内部函数为外部变量赋值?
function centermap(){
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('office_address').value;
var new_location = "";
geocoder.geocode({'address': address}, function(results, status, new_location) {
if (status == google.maps.GeocoderStatus.OK) {
new_location = results[0].geometry.location;
console.log(new_location); // output is fine here
}
else {
console.log("Geocode was not successful for the following reason: " + status);
}
})
console.log(new_location); // output is "" - which is the init value
return new_location // the returned object is also ""
};
$("input[id=office_address]").change(function(){
var coordinates = new Array();
var location = centermap();
coordinates.push(location.geometry.location.lat());
coordinates.push(location.geometry.location.lng());
map.setView(coordinates, 14);
});
什么我没有得到有关这里的范围?如何将“外部”new_location
设置为地址结果? 请随意点上这个
编辑对我的理解所有的错误:我看过的答案上this和this这样的问题,但我没能做到我想要的。
这里最重要的一点是,'geocoder.geocode'是异步的。 –