5

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?

JsusSalv
  • 517
  • 1
  • 8
  • 22
  • 1
    You can't return anything from an asynchronous callback function, you need to use the data there when/where it exists – geocodezip May 21 '16 at 00:39
  • 1
    Then what's the point of this documentation: https://developers.google.com/maps/documentation/javascript/geocoding It mentions everything that is returned, including the geometry: {location: LatLng}... which is what I need. – JsusSalv May 21 '16 at 00:47
  • Why the downvote? It's a legitimate question. If you don't understand the issue or the documentation then ask a question. This is a public API and others have had issues with it which have not been addressed properly. – JsusSalv May 21 '16 at 00:58
  • 2
    Related/possible duplicate question: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). The lat/lng **are** returned (if the result is "OK"), they are just returned in the callback function **after** the value of `coords` is returned to the calling function. – geocodezip May 21 '16 at 01:08
  • 1
    Good link but that's what I was referring to. It sounds like it may be an async issue but I don't understand how to fix that. How does passing the results to another function give Google more time to respond with the full info requested? I can't imagine I am the only dev to ever come across this problem. There's just no info online with examples on a possible fix. – JsusSalv May 21 '16 at 01:21
  • 1
    One option (which is described in the linked question) is a promise. – geocodezip May 21 '16 at 01:28
  • actually, you're right. I'm stuck using only certain methods, however. I can't implement jQuery, for example or something more suitable like promises. I'm looking at https://xhr.spec.whatwg.org/ Thanks for the help. – JsusSalv May 21 '16 at 01:48
  • 1
    Then use the asynchronous callback in the manner for which it was created (use the data in the callback function, when/where it is available, don't try to return it). – geocodezip May 21 '16 at 05:39

2 Answers2

3

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
coffee
  • 31
  • 3
  • Thank you! This is a great solution as well. Note: doesn't matter how long ago questions were posted. I am always grateful for answers/suggestions/ideas. Thank you!! – JsusSalv Jul 31 '21 at 17:47
1

Ended up using this to solve the issue:

var request = new XMLHttpRequest();
request.open( 'GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address, false);
request.send( null );

Though, I had to set false in order to keep the request synchronous. Seems this is/will be deprecated but for my purposes it should be fine. I can now parse through the json by doing the following:

var request = new XMLHttpRequest();
request.open( 'GET', 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address, false);
request.send( null );

This is fine I think. Just wish the Geocoder documentation made note of the asynchronous issues and provided better solutions.

JsusSalv
  • 517
  • 1
  • 8
  • 22
  • This is not a good idea, you should call a (new) function at the end of the anonymous function there coords gets values. – miguev Aug 26 '16 at 13:11
  • 1
    If you have a code sample to share please do so to prove your point. The above worked just fine since this isn't a codebase open to anyone except for this internal project. We've documented the limitation and workaround. So yes, it was a good idea so that we could move forward. – JsusSalv Aug 26 '16 at 17:13
  • This one does a few things with geocoding results: https://google-developers.appspot.com/maps/documentation/utils/geocoder/ – miguev Aug 28 '16 at 05:32