简体   繁体   English

从 place_id 获取 Google 地图链接

[英]Getting Google Maps link from place_id

HeIIo, using google map API with search I'm able to find a certain place and then store it's details. HeIIo,使用谷歌 map API 进行搜索,我能够找到某个地方,然后存储它的详细信息。 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;通过 API 我收到 place_id ,我觉得应该足以建立一个完整的谷歌地图链接; however, I can't find a way to do that.但是,我找不到这样做的方法。 Can anyone advise me on how to do that.任何人都可以建议我如何做到这一点。

Thank you for your time.感谢您的时间。

Try below syntax.试试下面的语法。 I hope it helps我希望它有帮助

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

Here is an official url to search for a placeId with a fallback to an address if the placeId does not exist如果placeId不存在,这是一个官方网址,用于搜索带有回退到addressplaceId

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无需令牌,适用于 Android、iOS(以及 iOS 11)和网络

There is a way of always getting the right URL but it does require an additional request.有一种方法可以始终获取正确的 URL,但它确实需要额外的请求。

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您可以使用 place_id 通过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.来自它的响应有一个 ['result']['url'] 字段,它总是打开正确的位置。 eg.例如。 https://maps.google.com/?cid=10254754059248426663

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). https://www.google.com/maps/place上没有有关预期参数的文档,因此这可能只是一种解决方法(至少它目前对我有用)。

The following URL-format seems to give the desired result in most cases:在大多数情况下,以下 URL 格式似乎可以提供所需的结果:

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您可以根据地名和地点几何创建此 URL

 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>

You can use google embed API, and take src as a link: eg:您可以使用 google embed API,并将 src 作为链接:例如:

 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文档: https ://developers.google.com/maps/documentation/embed/guide#place_mode

What worked for me without place_id没有 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

I had the same problem with an iOS app redirecting users to the desired place (returned from nearbysearch API) in google maps. nearbysearch应用程序在谷歌地图中将用户重定向到所需位置(从附近搜索 API 返回)时遇到了同样的问题。

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.如果您在浏览器中打开此链接,它将在 google map 网站上显示一个位置。

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.但是如果您尝试在手机上打开此链接,它将打开 google map 应用程序(如果它存在于用户的手机上)并且不会重定向到 safari。 Also, the google map application doesn't show the place ( that link will work on the browser only ).此外,谷歌 map 应用程序未显示该位置(该链接仅适用于浏览器)。

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.在测试了创建链接以在 google map 应用程序和浏览器中工作的多种方法之后,最好的方法是显示一个地方只是在链接中传递locationplace_id

let url = "https://www.google.com/maps/search/?api=1&query=\(placeLatitude)%2C\(placeLongitude)&query_place_id=\(placeId)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM