6

I found out, while using Google Maps API, the results change from time to time. By change I mean the response object. Basically I'm querying Google Maps to get a list of places based on user input (also using jQueryUI auto complete). I'm trying to correctly fetch the coordinates which get returned from Google Maps API but the properties containing the exact|approximate coordinates seem to change from time to time.

For example: in earlier days I was able to get coordinates like item.geocode.geometry.location.Ya and item.geocode.geometry.location.Za. Then my search form broke because Google changed it to item.geometry.location.mb and item.geometry.location.nb.

Any suggestions on how to implement this the correct, and stable way?

Ben Fransen
  • 10,884
  • 18
  • 76
  • 129

1 Answers1

14

Never use undocumented properties of a Maps API object like the ones you found. Those will change wildly whenever the Maps API is revised.

Instead, use only documented methods and properties.

Your item.geocode.geometry.location is a LatLng, so you can call its .lat() and .lng() methods:

var location = item.geocode.geometry.location;
var lat = location.lat();
var lng = location.lng();
Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • In V3 of the API, I used: `var lat = location.nb; var lng = location.ob;` – Imperative Nov 11 '13 at 01:58
  • 4
    @Imperative - No, that's exactly what you should *not* do. You're using undocumented properties that you found in the debugger. They may work this week, but they will change randomly whenever Google recompiles the Maps API code. Use the documented `lat()` and `lng()` methods instead. – Michael Geary Nov 11 '13 at 08:42
  • Damn, that was easy. Thanks Michael! – Imperative Nov 12 '13 at 03:35