Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Geocoding APi not returning lat and long

I'm utilizing the Geocoding API for converting and address to latitude and longitude coordinate values. I do receive results but the lat and long are empty. Other SO posts mention the Geocoder requests are asynchronous and Google just doesn't return the data fast enough. I'm not understanding how to fix this for my code if that is the problem.

var coords = [];
var address = '1600 Pennsylvania Ave NW, Washington, DC 20500';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function( results, status ) {
        if (status === google.maps.GeocoderStatus.OK) {
            coords[0] = results[0].geometry.location.lat();
            coords[1] = results[0].geometry.location.lng();
        }
        else {
            coords = 'Could not retrieve coordinates for: ' + address;
        }
    });
return coords;

Even if I use a well-known address I still get no lat or long.

Results object:

enter image description here

Is this really the problem or is there something wrong with the code I'm using? How can I fix this?

like image 533
JsusSalv Avatar asked Oct 18 '22 09:10

JsusSalv


1 Answers

this question was asked a long time ago but your code gave me hint to solve my problem. The response returned from API call are only function lat & function lgt, that's why there are no coordinates. You have to call the functions as specified in the question

coords[0] = results[0].geometry.location.lat();
coords[1] = results[0].geometry.location.lng();

if you console.log(coords), you can see the result.

like image 104
coffee Avatar answered Nov 15 '22 07:11

coffee