var geocoder, map, point, fmtAdd, marker;
function mapLoad() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 15,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
address="W3 6BY";
if(address){geocoder.geocode({'address':address}, geocodeResult);}
else{alert("Postcode Incorrect");window.close();}
}
function geocodeResult(results, status) {
if (status == 'OK' && results.length > 0) {
point=results[0].geometry.location;
map.setCenter(point);
marker = new google.maps.Marker({map: map, position: point, draggable: true});
geocoder.geocode({latLng:point},function(results, status){
if(status == 'OK') {
if(results.length == 0) {
fmtAdd = 'None';
} else {
fmtAdd = results[0].formatted_address;
}
} else {
fmtAdd = 'Error';
}
alert(fmtAdd); // shows the address
});
alert(fmtAdd); // says undefined;
} else {
alert("Error: " + status);
}
}
mapLoad();
我想显示来自英国用户输入的格式化地址。但我不明白为什么第二个警报未定义?我不是在第一行定义了变量“fmtAdd”吗?