从Google Maps API输出Google地理编码响应

时间:2011-05-02 18:54:21

标签: javascript google-maps maps google-api

我正在使用此处记录的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);
            }
        }
    );
}

1 个答案:

答案 0 :(得分:4)

你在回调中返回一个变量 - 但是这个回归在哪里? output未定义,因为您在调用localPoint.geocode()之后立即发出警报,而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);


        });
     }

Here is a working example

循环中的所有内容必须包含在回调中,以便在地理编码成功后才能工作。

基本上,我们将当前索引和回调传递给地理编码功能。在地理编码回调中,我们在全局上下文中调用我们的回调函数(原始循环内的所有内容)(第一个变量是函数将在其中运行的上下文 - 我们使用window以便它可以访问所有全局定义的变量)。我们还将这一点传递给回调和当前索引,因为我们不知道何时将执行回调,因此我们需要确保它在运行时具有所需的一切。如果我们没有传递索引,那么循环就会完成,然后结束的任何i都会在依赖它的语句中使用。