I'm using Google's geocding service as documented here (http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding)
I'm trying to get the result into an external variable but keep getting "undefined".
Here's my code:
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.loca开发者_StackOverflow社区tion;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
alert(output)
*(Updated) - Ideally I would be able to wrap it in a function so I could just return the results like this:
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);
}
}
);
}
You are returning a variable in a callback - but where is this return going? output
is undefined, because you are alerting it right after calling localPoint.geocode(), which may NOT HAVE COMPLETED YET. Therefore, you need to place your alert (and in general, ANY code that depends on the result) INSIDE your callback:
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);
}
});
Now, if you have other code that needs to use the geocode results, make sure to make the function calls inside of the callback:
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)
will only be called if the geocode was succesful.
UPDATE: ok, I've taken a look at the code you linked to. You are trying to geocode in a loop. Can be done with a few modifications to your code. Specifically, you'll need to make the following changes:
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);
}
});
}
and the loop will look like this:
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
Everything in the loop must be wrapped in the callback for it to work once the geocode is successful.
Basically, we pass the current index and the callback to the the geocoding function. Inside the geocode callback we are calling our callback function (everything inside of your original loop) in the global context (the first variable is the context that the function will run inside - we use window
so that it has access to all of your globally-defined variables). We also pass the point to the callback and the current index because we don't know when the callback will execute and so we need to make sure it has everything it needs when it runs. If we didn't pass the index the loop would finish and then whatever i
ended at would be used in statements that depend on it.
精彩评论