8

I have a place id obtained from Google Places API, and the aim is opening Google Maps app via place id like similar approach of below

Uri gmmIntentUri = Uri.parse(String.format(Locale.ENGLISH,"geo:%f,%f", lat, lon));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mapIntent);
}

Is there a way for this ?

I have already latitude, longitude when user type for example "Eiffel Tower" from places API. And I'm storing those values for next usages(Say we are listing some sort of visited places from local database, and user click one of them and app navigates to Google Maps)

I have a few concerns

  1. If i use latitude-longitude to indicate a place instead of place id on Google Maps, what will happen if "Cafe XYZ" moved another street ?

  2. If i use place id each time to receive last location of "Cafe XYZ" (instead of stored values in my local database) the app easily hit request limits

So those concerns made me think a way that just sending place id Google Maps(like sending coordinates)

blackkara
  • 4,900
  • 4
  • 28
  • 58
  • `GooglePlacesAPI` also provide `latitude` and `longitude` with `placeid`. So use `lat-lng` provide by `GooglePlacesAPI` to open map instead of `placeid`. This is simple. – Aditya Jan 27 '18 at 11:01
  • @Heisen-Berg Thanks for comment. I just modified the question to explain WHAT and WHY clearly. It also includes response for your comment. – blackkara Jan 27 '18 at 12:23

2 Answers2

15

Indeed, there is a way to open Google Maps app with a place ID. For this purpose you have to use Google Maps URLs that was launched in May 2017. Following the documentation of Google Maps URLs you can construct the following URL for "Eiffel Tower" (place ID ChIJLU7jZClu5kcR4PcOOO6p3I0)

https://www.google.com/maps/search/?api=1&query=Eiffel%20Tower&query_place_id=ChIJLU7jZClu5kcR4PcOOO6p3I0

So, your code will be something like

Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/search/?api=1&query=Eiffel%20Tower&query_place_id=ChIJLU7jZClu5kcR4PcOOO6p3I0");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mapIntent);
}

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117
  • Thanks, it's super. But it doesn't work with sending only place id (didn't understand this why!) Hopefully i had stored place names before. – blackkara Jan 28 '18 at 09:19
  • 1
    Even if you didn't store place names, just use some random value for `query` parameter and it should work, because the place ID is more important. For example `https://www.google.com/maps/search/?api=1&query=qwerty&query_place_id=ChIJLU7jZClu5kcR4PcOOO6p3I0` will return "Eiffel Tower", although I sent rubbish `query=qwerty` parameter. – xomena Jan 28 '18 at 15:14
  • For now i have just tried `https://www.google.com/maps/search/?api=1&query=qwerty&query‌​_place_id=ChIJLU7jZC‌​lu5kcR4PcOOO6p3I0` on web(on browser), but returned `qwerty` related values. Seems like giving more priority for query than place id(i'm confused) – blackkara Jan 28 '18 at 15:57
  • You are right. This is not reliable. It looks like you need both Place name and place ID in order to create this URL. – xomena Jan 28 '18 at 16:01
  • Please read section Map actions -> parameters in this link https://developers.google.com/maps/documentation/urls/guide#search-action and you will know everything about these parameters – Mkr Aug 02 '18 at 11:21
6

It's probably super late but I was able to make it work with info we should have aside from the LatLog Searching for CenturyLink Field using latitude/longitude coordinates as well as the place ID results in the following map:

https://www.google.com/maps/search/?api=1&query=47.5951518,-122.3316393&query_place_id=ChIJKxjxuaNqkFQR3CK6O1HNNqY

enter image description here

Carlos Rojas
  • 163
  • 1
  • 9
  • thanks for your info. nothing special but it still works with any query like 11111 https://www.google.com/maps/search/?api=1&query=11111&query_place_id=ChIJKxjxuaNqkFQR3CK6O1HNNqY – wwwrrrxxx Nov 22 '19 at 22:17