0

Is there any way to retrieve different levels of placeIds from a specific placeId? Like when you load the address components of a place, you get 'country' 'city' 'region' and so on... But you only get the names of the different levels of position. Is it possible to get the placeId of the country, city.... ?

When I am storing data in the database, I need to also store the country placeId, city placeId together with the original placeId. Do not want to store the name of the places in the database, when a lot of places have the same names.

1 Answers1

0

As you noticed there are no place IDs in address components array. You cannot get this information executing only place details request. Developers have already created a feature request in Google issue tracker:

Add place_id in each address_components

You can star it to add your vote. In the meantime the workaround to get place IDs for country and locality consists in executing a reverse geocoding requests for coordinates of the original place ID with result type locality and country.

For example, I have a place ID ChIJEygrJmSYpBIRUXOjvF0QSLw. The details request for this place

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJEygrJmSYpBIRUXOjvF0QSLw&key=MY_API_KEY

gives coordinates 41.3886875,2.130554. Now I can execute additional reverse geocoding request to get place IDs of locality and country:

https://maps.googleapis.com/maps/api/geocode/json?latlng=41.3886875%2C2.130554&result_type=country%7Clocality&key=MY_API_KEY

The last one gives me

  • place ID ChIJ5TCOcRaYpBIRCmZHTz37sEQ (Barcelona)
  • place ID ChIJi7xhMnjjQgwR7KNoB5Qs7KY (Spain)

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117
  • Thank you, trying to get data by reverse geocoding with coordinates using Swift. But I am getting errors? It is allowed to retrieve JSON data like that right? By just using the Url? I have done the setup of adding API keys to GMSPlacesClient and GMSServices. Have also enabled maps API in the cloud console, but still getting errors when launching in Xcode. Is there something I am missing? – Kristian Lofthus Jun 07 '18 at 22:24
  • The error message: "CoreData: annotation: Failed to load optimized model at path '/Users/user/Library/Developer/CoreSimulator/Devices/ID/data/Containers/Bundle/Application/ID/Project/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileProto.omo'" – Kristian Lofthus Jun 07 '18 at 22:26
  • You can send HTTP requests from swift as described in https://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift. API key for web service should be different from one you used in Maps iOS SDK, because web service doesn't support iOS app restrictions. – xomena Jun 08 '18 at 07:37
  • Referring to the 'Failed to load optimized model at path' have a look at https://issuetracker.google.com/issues/64504919 – xomena Jun 08 '18 at 07:39