我正在使用此处记录的 Google 地理定位服务 (http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding)
我正在尝试将结果放入外部变量,但一直显示“未定义”。
这是我的代码:
localPoint = new google.maps.Geocoder();
output = localPoint.geocode( { 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
alert(output)
*(更新)- 理想情况下,我可以将它包装在一个函数中,这样我就可以像这样返回结果:
localPoint = new google.maps.Geocoder();
function codeAddress(this_address) {
localPoint.geocode(
{ 'address': this_address},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}
);
}
最佳答案
您正在回调中返回一个变量——但是这个返回去哪里了? output
未定义,因为您在调用 localPoint.geocode() 之后立即提醒它,这可能尚未完成。因此,您需要将警报(通常是任何依赖于结果的代码)放在回调中:
localPoint = new google.maps.Geocoder();
localPoint.geocode( { 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var output=results[0].geometry.location;
alert(output);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
现在,如果您有其他代码需要使用地理编码结果,请确保在回调内进行函数调用:
function processResults(location){
//do stuff with a successful geocode here
}
localPoint = new google.maps.Geocoder();
localPoint.geocode( { 'address': "1009 south 10th Ave, Kelso WA 98626"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
processResults(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
processResults(results)
只有在地理编码成功时才会被调用。
更新:好的,我已经查看了您链接到的代码。您正在尝试循环地理编码。可以通过对代码进行一些修改来完成。具体来说,您需要进行以下更改:
function codeAddress(this_address,index,callback) {
geocoder.geocode( { 'address': this_address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback.call(window,index,results[0].geometry.location)
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
循环看起来像这样:
for (var i = 0; i < businesses.length; i++) {
//var point = new google.maps.LatLng(businesses[i].lat,businesses[i].lng);
codeAddress(businesses[i].address,i,function(i,point){
var description = businesses[i].description;
if(businesses[i].business_type == "Wine"){
//http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|00CC99|000000
var icon = 'http://google-maps-icons.googlecode.com/files/wineyard.png';
}else if(businesses[i].business_type == "Golf"){
var icon = 'http://google-maps-icons.googlecode.com/files/golf.png';
}else{
var icon = 'http://google-maps-icons.googlecode.com/files/festival.png';
}
var marker = createMarker(point,businesses[i].name,description,icon);
});
}
一旦地理编码成功,循环中的所有内容都必须包含在回调中才能正常工作。
基本上,我们将当前索引和回调传递给地理编码函数。在地理编码回调中,我们在全局上下文中调用我们的回调函数(原始循环内的所有内容)(第一个变量是函数将在其中运行的上下文 - 我们使用 window
以便它具有访问所有全局定义的变量)。我们还将点传递给回调和当前索引,因为我们不知道回调何时执行,因此我们需要确保它在运行时具有所需的一切。如果我们不传递索引,循环就会结束,然后 i
结束的任何内容都将在依赖它的语句中使用。
关于javascript - 从谷歌地图 API 输出谷歌地理编码响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861166/