0

I am trying to use a simple below function from jQuery function:

var getLocation =  function(address) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            console.log('coordinates: ' + latitude, longitude);
            return {latitude: latitude, longitude: longitude};
        }
        console.log('maps are not OK');
        return undefined;
    });
}

I see the coordinates printed in the browser console.

However, the function calling it gets undefined. Below is my invocation:

var loc = getLocation($('#origin').val());

The loc is always undefined, even though inside getLocation() coordinates appear to be fine.

Please advise what I am doing wrong. Tried silly things, like adding quotes around latitude and longitude etc. Does not seem to make a difference.

Thank you.

Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43

0 Answers0