So I’m making a Google Maps based webapp in JavaScript and a part of my code looks like this:
function revGeocode(marker){
var latlng = marker.position;
if (geocoder) {
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
return "GOOD";
} else {
alert("No results found");
return "BLAH1";
}
} else {
alert("Geocoder failed due to: " + status);
return "BLAH2";
}
});
}
else{
return "No Geocoder?!"
}
return "Weird..";
}
Now, for some reason it’s skipping both the IF and ELSE conditions and jumping to the final return (which I just added to see why it wasn’t catching both conditions). Anyone know why this is happening?
If I’m right about what you’re expecting to happen…
You’re returning from the anonymous function when the geocoder receives a positive response from the Google Maps API service, but that returns from the anonymous function to the caller (whatever the geocoder object uses to dispatch notifications to your callback function), it doesn’t return from your
revGeocode(marker)method.So, if you’re expecting to see
"GOOD"somewhere, that won’t happen. If you put analert("GOOD");there instead, that should be triggered in a noticeable way. Currently, you should only see something if things don’t go as planned, but it seems like things must be working out correctly.After that, there are no other
returnstatements in your initial function’s codepath after your call togeocoder.geocode(), so the returned value will be"Weird..".Hopefully that’s helpful, if I’ve misunderstood what you were asking about, let me know.