Places Autocomplete API to get GPS coordinates from address entered

simha picture simha · Feb 18, 2013 · Viewed 11.6k times · Source

I have an address field in my app where the user needs to enter the required address. I have used google Geocoder to get the GPS coordinates of the address . But now I want to make it easier for the user by using Places Autocomplete . But Places Autocomplete only returns the address , Id and reference of the place .

Is there a way to get the GPS coordinates of the address selected by the user using Places Autocomplete API ? Do I have to use Geocoder again after the user selects his address from Places Autocomplete ?

Or Should I use Places API again after the user selects his address to get the GPS coordinates of the address ? I dont want to send multiple requests to the Places API because of the usage limits in place . . This is the expected response from Places API ..There is no GPS coordinates in the response .

"status": "OK",
  "predictions": [ {
    "description": "Paris, France",
    "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
    "reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "France",
      "offset": 7
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }, {
    "description": "Paris, TX, United States",
    "id" : "518e47f3d7f39277eb3bc895cb84419c2b43b5ac",
    "reference": "CjQjAAAAHnbxZZ...BDR3iIOFdMTxwo1jHg",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "TX",
      "offset": 7
    }, {
      "value": "United States",
      "offset": 11
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }, {
    "description": "Paris, Ontario, Canada",
    "id" : "e7ac9c89d4a590305242b0cb5bf43064027223c9",
    "reference": "CjQhAAAAIv_YWYt...F8KZHY36TwMrbyu_g",
    "terms": [ {
      "value": "Paris",
      "offset": 0
    }, {
      "value": "Ontario",
      "offset": 7
    }, {
      "value": "Canada",
      "offset": 16
    } ],
    "types": [ "geocode" ],
    "matched_substrings": [ {
      "offset": 0,
      "length": 5
    } ]
  }

Answer

thomasskov picture thomasskov · Feb 21, 2013

You simply use the Place Details part of the Places API where you get the actual place from the reference in each suggestion using the "reference" value. For example:

https://maps.googleapis.com/maps/api/place/details/json?reference=CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc&sensor=true&key=AddYourOwnKeyHere

This will give you information about the place including its coordinates (point for a specific location, bounds for an area).