简体   繁体   English

单击POI时,在Google地图上获取placeId

[英]Get placeId on google maps when POI is clicked

I'm using Google Maps JS V3 API on my website. 我在我的网站上使用Google Maps JS V3 API。 I'm able to use getDetails by placeId when user searches for a place. 当用户搜索某个地方时,我可以通过placeId使用getDetails。 I would like to do the same when user clicks on a POI. 当用户点击POI时,我想做同样的事情。 However, I can't seem to find a way to get this placeId when user clicks on a POI instead of using the search box. 但是,当用户点击POI而不是使用搜索框时,我似乎无法找到获取此placeId的方法。

I have done research for days, and had no luck of finding anything close. 我做了几天的研究,没有找到任何接近的运气。

I came across this feature request, I wonder if there is really no way of getting the placeId by POI clicking: https://code.google.com/p/gmaps-api-issues/issues/detail?id=8113&q=poi&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal 我遇到了这个功能请求,我想知道是否真的无法通过POI点击获取placeId: https//code.google.com/p/gmaps-api-issues/issues/detail? id = 8113&q = poi&solpec = ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

Google doesnt provide any documented API method to get place id by clicking on a POI. Google没有提供任何记录的API方法来通过点击POI获取地点ID。 However, I was able to get the place id using reverse geocoding. 但是,我能够使用反向地理编码获取地点ID。

First, we can get coordinates of POI by a method described in this Stack-overflow answer 首先,我们可以通过此Stack-overflow答案中描述的方法获取POI的坐标

Second, you can use the coordinates from getPosition() method to call the geocode method to retrieve address components and place id. 其次,您可以使用getPosition()方法中的坐标来调用地理编码方法来检索地址组件并放置id。

Here is a working JS Fiddle 这是一个工作的JS小提琴

 function initialize() { var mapOptions = { zoom: 14, center: new google.maps.LatLng(38.8862447, -77.02158380000003), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); geocoder = new google.maps.Geocoder; //keep a reference to the original setPosition-function var fx = google.maps.InfoWindow.prototype.setPosition; //override the built-in setPosition-method google.maps.InfoWindow.prototype.setPosition = function() { //this property isn't documented, but as it seems //it's only defined for InfoWindows opened on POI's if (this.logAsInternal) { google.maps.event.addListenerOnce(this, 'map_changed', function() { var map = this.getMap(); //the infoWindow will be opened, usually after a click on a POI if (map) { //trigger the click google.maps.event.trigger(map, 'click', { latLng: this.getPosition() }); } }); } //call the original setPosition-method fx.apply(this, arguments); }; google.maps.event.addListener(map, 'click', function(e) { //alert('clicked @' + e.latLng.toString()) geocoder.geocode({ 'location': e.latLng }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { if (results[0]) { alert('place id: ' + results[0].place_id); } else { console.log('No results found'); } } else { console.log('Geocoder failed due to: ' + status); } }); }); } google.maps.event.addDomListener(window, 'load', initialize); 
 html, body, #map_canvas { margin: 0; height: 100%; } 
 <div id="map_canvas"></div> <script src="https://maps.googleapis.com/maps/api/js?callback=initialize" async defer></script> 

As of 2017-12 this is now supported. 截至2017年12月,现在支持此功能。 Here's the example https://developers.google.com/maps/documentation/javascript/examples/event-poi 以下是https://developers.google.com/maps/documentation/javascript/examples/event-poi示例

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

相关问题 如何从 Google 地图 URL 获取 Google Place ID (placeId) - How to get a Google Place ID (placeId) from a Google Maps URL componentRestrictions无法在Google地图的反向地址编码中与placeId一起使用 - componentRestrictions is not working with placeId in reverse Geocoding in google maps 谷歌地图上使用json和placeid的多个标记 - Multiple markers on google maps using json and placeid 从附近搜索获取PlaceID并显示在标记InfoWindow(JS Google Maps API)中 - Get PlaceID from nearbySearch and display in a marker InfoWindow ( JS Google Maps API ) 单击时如何获取 id 并删除它? 谷歌地图 V3 - How to get the id when is clicked and remove that? Google Maps V3 Google Maps API Geocoder不会按placeId返回位置 - Google Maps API Geocoder doesn't return location by placeId Google Maps API(JS)在航点中使用PlaceId创建路线 - Google Maps API (JS) Create a route using PlaceId in waypoints 当我单击谷歌地图时如何获取鼠标的偏移量(在地图上的左侧和顶部) - how to get the mouse's offset(left and top on the maps) when i clicked the google maps 获取谷歌地图中单击标记的完整地址 - Get full address of clicked marker in google maps 获取 Google 地图中的 Clicked 标记并将其发送给函数 - Get the Clicked marker in Google maps and send it to a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM