Getting Google Maps link from place_id
Asked Answered
S

8

52

HeIIo, using google map API with search I'm able to find a certain place and then store it's details. However, the array doesn't contain google maps link which would open this place on google maps.

Via API I receive place_id which, as I feel, should be enough to build a full google maps link; however, I can't find a way to do that. Can anyone advise me on how to do that.

Thank you for your time.

Scorecard answered 16/6, 2015 at 0:41 Comment(2)
I'm having this same issue in an Android app I'm building. If the answer below is correct, it seems ridiculous to have to use the returned place_id to create another URL request to get the correct CIDLongmire
I found this tool, which is based on the answers below, to be extremely useful: Google Maps Place ID Opener.Corduroys
G
169

Try below syntax. I hope it helps

https://www.google.com/maps/place/?q=place_id:ChIJp4JiUCNP0xQR1JaSjpW_Hms
Great answered 15/5, 2017 at 0:23 Comment(6)
Simple, with no additional request to the server.Posit
Google maps on android says: Unsupported link: Google maps can't open this linkHerbie
@KirillKulakov sorry for this.. :(Great
Confirmed working solution August 2022Kraska
@Kraska It doesn't work on Android. See Kirill Kulakov's answer for an alternative that works on both desktop and mobile. Any string for "query" will work, it doesn't have to be the address.Sourpuss
Now how do you do it the other way round? Maps page to place_id?Livvie
H
44

Here is an official url to search for a placeId with a fallback to an address if the placeId does not exist

https://www.google.com/maps/search/?api=1&query=<address>&query_place_id=<placeId>

no token required, works on Android, iOS (as well as iOS 11) and web

Herbie answered 23/8, 2018 at 20:14 Comment(5)
Confirmed - works well! The address part is required but doesn't do anything it seems. I was able to enter anything in there and have the lookup still work.Selfdeception
The address is a backup, if the google place ID will be delete, google maps will fallback to the address. you can try that by providing an invalid place idHerbie
Best solution - on desktop opens Google Maps website, on mobile (Android and iOS) opens Google Maps application.Lett
This solution works, thanks, but I get "Mixed content" warnings when I eventually land on the listing: Mixed Content: The page at 'https://www.google.com/maps/search/?api=1&query=%3Caddress%3E&query_place_id=ChIJoT5E1FlgLxMR1qP3Z4NRVBY' was loaded over HTTPS, but requested an insecure image 'http://www.google.com/maps/vt/data=zYuhVnS8JosXRxoH3bbPoEAByijPZHC_yNruvFpujaxqT6-OKmrS1cV2ft0f8dQNh7_HX2epahFbnLuLDgS39EdmrW_ZHt-eAfKRxDSXoaOf3BHtn5RaLfE26v_G06ja4OLDXnSi_nubeVjJPp0'. This content should also be served over HTTPS.Semipalatinsk
query is required based on the docs developers.google.com/maps/documentation/urls/get-startedPreterition
D
13

There is a way of always getting the right URL but it does require an additional request.

You can use the place_id to get the place details with https://maps.googleapis.com/maps/api/place/details/json?key=YOURAPIKEY&placeid=THEPLACEID

The response from that has a ['result']['url'] field which always opens up the right place. eg. https://maps.google.com/?cid=10254754059248426663

Deposition answered 12/1, 2016 at 14:34 Comment(2)
I'm getting INVALID REQUEST with thatMonumental
You need to use ServerAPIKey. Loot at this answer: https://mcmap.net/q/180142/-this-ip-site-or-mobile-application-is-not-authorized-to-use-this-api-keyWarner
M
12

There is no documentation for the expected parameters on https://www.google.com/maps/place , so this may only be a workaround(at least it currently works for me).

The following URL-format seems to give the desired result in most cases:

https://www.google.com/maps/place/[place-name]/@[latitude],[longitude],[zoom]z/

You may create this URL based on the place-name and the place-geometry

function initialize() {
  var ac = new google.maps.places.Autocomplete(document.getElementById('pac'));
  google.maps.event.addListener(ac, 'place_changed', function() {
    var place = this.getPlace(),
      link = document.getElementById('link');
    if (!place.geometry) {
      link.textContent = '';
    } else {
      var zoom = 17,
        url = 'https://www.google.com/maps/place/' +
        encodeURIComponent(place.name) + '/@' +
        place.geometry.location.toUrlValue() + ',' +
        zoom + 'z/';
      link.href = link.textContent = url;

    }

  });
}
google.maps.event.addDomListener(window, 'load', initialize);
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places">
</script>
<input id="pac" />
<br/>
<a id="link"  href="" target="_blank"></a>
Morrill answered 16/6, 2015 at 6:3 Comment(0)
T
4

I had the same problem with an iOS app redirecting users to the desired place (returned from nearbysearch API) in google maps.

They mentioned above:

https://www.google.com/maps/place/?q=place_id:ChIJp4JiUCNP0xQR1JaSjpW_Hms

If you open this link in your browser, it will show a place on the google map website.

But if you try to open this link on a mobile phone, it will open the google map application instead (if it exists on the user's phone) and not redirect to safari. Also, the google map application doesn't show the place (that link will work on the browser only).

After testing many ways for creating a link to work both in the google map application and browser, the best way is to show a place is just passing location and place_id within the link.

let url = "https://www.google.com/maps/search/?api=1&query=\(placeLatitude)%2C\(placeLongitude)&query_place_id=\(placeId)"
Toddle answered 9/9, 2022 at 6:28 Comment(0)
S
1

You can use google embed API, and take src as a link: eg:

 https://www.google.com/maps/embed/v1/place?key=YOUR_API_KEY
  &q=place_id:YOUR_PLACE_ID

Docs: https://developers.google.com/maps/documentation/embed/guide#place_mode

Strike answered 24/11, 2016 at 11:58 Comment(1)
This requires an API key, which isn't ideal for a lot of people.Bb
L
1

You can use google embed API, and take src as a link: eg:

https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJN1t_tDeuEmsRUsoyG83frY4&key=YOUR_API_KEY

Latona answered 6/5, 2020 at 7:14 Comment(0)
F
0

What worked for me without place_id

https://www.google.com/maps/search/<search term>/@<coordinates>,<zoom level>z

Example

https://www.google.com/maps/search/Jouvay+Night+Club/@40.6961111,-73.8041667,15z
Faultfinding answered 2/3, 2021 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.