javascript 从 place_id 获取 Google 地图链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30857150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:48:50  来源:igfitidea点击:

Getting Google Maps link from place_id

javascriptgoogle-mapsgoogle-maps-api-3google-maps-urls

提问by A L

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.

你好,使用谷歌地图 API 进行搜索,我可以找到某个地方,然后存储它的详细信息。但是,该数组不包含可在 google 地图上打开此地点的 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.

通过 API,我收到了 place_id,据我所知,它应该足以构建一个完整的谷歌地图链接;但是,我找不到办法做到这一点。任何人都可以建议我如何做到这一点。

Thank you for your time.

感谢您的时间。

回答by Sedat Kumcu

Try below syntax. I hope it helps

试试下面的语法。我希望它有帮助

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

回答by Kirill Kulakov

Here is an official url to search for a placeIdwith a fallback to an addressif the placeIddoes not exist

这是一个官方网址,用于搜索placeId具有后备的 aaddress如果placeId不存在

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

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)和网络

回答by Dr.Molle

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>

回答by Bartek Juszczak

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. eg. https://maps.google.com/?cid=10254754059248426663

来自那个的响应有一个 ['result']['url'] 字段,它总是打开正确的位置。例如。https://maps.google.com/?cid=10254754059248426663

回答by TomoMiha

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

回答by Kavos Khajavi