JavaScript return issue


var geocoder = new google.maps.Geocoder();

function GetLatLngFromZip(zip) {
    return geocoder.geocode({ 'address': zip }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].geometry.location);
            return results[0].geometry.location
        }
    });
};


I must be missing something, but I can’t get the above function to return the result?

Does status != google.maps.GeocoderStatus.OK ?

That function will never return the result because geocoding is an asynchronous process within the context of gmap3. Which is why a callback is being passed upon calling the method. So when the geocoder sends back the data the callback is called allowing the outside world to do something with that value. The short of it is that the GetLatLngFromZip() function requires an additional argument that should be a function which pases the zipcode value once it has been resolved.

want to show me what that code would look like?


var geocoder = new google.maps.Geocoder();

function GetLatLngFromZip(zip,crapToDoAfterwards) {
    geocoder.geocode({ 'address': zip }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            crapToDoAfterwards(results[0].geometry.location);
        }
    });
};