Question

I've been successfully using the google api for geocoding, but when I originally set it up I noticed that the object returned is not quite the same as described in the google documentation here: https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses

I used console.dir to inspect the object in chrome.

Unless I've misunderstood (quite likely), I can retrieve the latitude and longitude for the supplied address as follows:

lat = results[0].geometry.location.lat;
lng = results[0].geometry.location.lng;

Except, that wasn't working, and when I investigated I found that lat and lng were at results[0].geometry.location.d and results[0].geometry.location.e respectively.

All good until today it stopped working. Investigating I found that lat & lng are being returned via results[0].geometry.location.k & results[0].location.A.

Presumably this will break again before too long.

If I inspect results[0].geometry.location.lat it contains this:

function (){
\"use strict\";
return this[a]}

Anyone recognise that?

Était-ce utile?

La solution

The results returned from the geocode-method of the Javascript-Geocoder is already translated to be used with the Javascript-API(the linked documentation is for the Geocoding-Webservice, not for the Maps-Javascript-API).

geometry.location is not a plain object, it's a google.maps.LatLng

you must use the methods lat() and lng() of geometry.location to retrieve latitude and longitude.

Specification of GeocoderGeometry

In your case that would mean using it like this:

lat = results[0].geometry.location.lat(); etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top