0

I've got this code:

var get_lat = function(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log('lat is: '+ results[0].geometry.location.lat());
            return results[0].geometry.location.lat();
        }
    });
}

var get_lng = function(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log('lng is: '+ results[0].geometry.location.lng());
            return results[0].geometry.location.lng();
        }
    });
}

In console it prints the coordinates I need, but the return value is always undefined:

I use it in classic initialize for Google Maps like this:

function createMarker(latlng) {
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
    });
}

function initialize() {
    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(49.210366,15.989588)
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var address = "Correct address that console.logs the correct GPS coordinations in function definitions above";
    var gps_lat = get_lat(address);
    var gps_lng = get_lng(address);
    console.log('GPS lat: ' + gps_lat); // logs "undefined"
    console.log('GPS lng: ' + gps_lng); // logs "undefined"

    var marker = createMarker({lat: gps_lat}, lng: gps_lng});
}


$(window).on("load", function (e) {  
    initialize();
});

Do you have any idea why the function consoles the right value but it doesn't return anything?

For GMAP API I use this script: http://maps.google.com/maps/api/js?sensor=false&libraries=geometry

Café
  • 98
  • 1
  • 7

2 Answers2

1

You have the return inside an anonymous function that you pass to geocoder.geocode, all in yet another function (get_lat/get_lng). get_lat/get_lng themselves don't have a return statement, and thus return undefined.

Furthermore, geocoder.geocode will call your anonymous function asynchronously. Which means that there is no chance for get_lat/get_lng to get hold of the return value and return it where get_lat/get_lng was called.

One solution (the simplest one) is to put the createMarker code in the callback for geocoder.geocode. Also, in this case you will have to merge your two get_lat/get_lng functions. Example:

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var gps_lat = results[0].geometry.location.lat()
        var gps_lng = results[0].geometry.location.lng();
        console.log('lat is ' + gps_lat + '. lng is ' + gps_lng);
        var marker = createMarker({lat: gps_lat, lng: gps_lng});
        return results[0].geometry.location.lat();
    }
});
sneep
  • 1,828
  • 14
  • 19
0

You have to wait until Google API gives response. So write a logic to create marker in callback function of Google API as shown below.

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

                // Create marker once you get response from Google
                createMarker({ lat: location.lat(), lng: location.lng() });
            }
        });
    }

and call that function as shown below

function initialize()
    {
        var myOptions =
        {
            zoom: 8,
            center: new google.maps.LatLng(49.210366, 15.989588)
        }

        map = new google.maps.Map(document.getElementById("map"), myOptions);
        var address = "Correct address that console.logs the correct GPS coordinations in function definitions above";            

        // Old code ------------------------------------------------
        //var gps_lat = get_lat(address);
        //var gps_lng = get_lng(address);

        //console.log('GPS lat: ' + gps_lat); // logs "undefined"
        //console.log('GPS lng: ' + gps_lng); // logs "undefined"

        //var marker = createMarker({ lat: gps_lat, lng: gps_lng });
        // ----------------------------------------------------------

        // New code
        getLatLng(address);
    }

and you will get marker on a map

Advait Baxi
  • 1,530
  • 14
  • 15