0

I have two Javascript functions that try to find the latitude and longitude of an address using Google Maps API Geocode:

function getLatLon(address) {
    var location = -1;
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat();
            var lon = results[0].geometry.location.lng();
            location = new Array(lat, lon);
            //document.getElementById('results').innerHTML = location[0];
        } else {
            alert("Geocode was not successful.");
        }
    });
    return location;
}

function search() {
    var address = document.getElementById('address').value;
    var location = getLatLon(address);
    document.getElementById('results').innerHTML = location[0];
}

While the location[0] inside getLatLon() prints out the correct number in the #results div, the location[0] inside search() returns undefined. Do you have any ideas why this might happen? I've tried returning a plain string ("Hello") from getLatLon() instead and that works just fine.

4
  • 5
    Is geocoder.geocode asynchronous? If yes, getLatLon should always return -1. Always Commented Nov 26, 2012 at 4:02
  • Uh, I have a question. How can getLatLon returning undefined? location is -1, so it should be returning -1 as well. Commented Nov 26, 2012 at 4:07
  • 1
    @Derek - I think the problem is location[0] is undefined not getLatLon(). Poor question wording IMO. Commented Nov 26, 2012 at 4:10
  • possible duplicate of Not able to return Geocode latLng Commented Nov 26, 2012 at 4:16

2 Answers 2

6

The problem is geocoder.geocode(...) is asynchronous so you can't return a value. Try it like this instead:

function getLatLon(address, callback) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat();
            var lon = results[0].geometry.location.lng();
            location = new Array(lat, lon);
            callback(location);
        } else {
            alert("Geocode was not successful.");
        }
    });
}

function search() {
    var address = document.getElementById('address').value;
    getLatLon(address, function(location) {
        document.getElementById('results').innerHTML = location[0];
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Clearly I need a better understanding of callbacks. Thanks!
For anyone who has a similar problem, here's an article I used to understand callbacks.
2

According to the API docs, calling geocoder.geocode(request, callback_function) will return nothing right away. When the response has been received and processed, the callback_function will be called. Until then, location will be left at whatever value you set it.

You need to start the search itself in the call-back function when you call getLatLon. For flexibility, you might do this by adding a call-back parameter to getLatLon that will be called from within the geocode() call-back function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.