簡體   English   中英

單擊POI時,在Google地圖上獲取placeId

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

我在我的網站上使用Google Maps JS V3 API。 當用戶搜索某個地方時,我可以通過placeId使用getDetails。 當用戶點擊POI時,我想做同樣的事情。 但是,當用戶點擊POI而不是使用搜索框時,我似乎無法找到獲取此placeId的方法。

我做了幾天的研究,沒有找到任何接近的運氣。

我遇到了這個功能請求,我想知道是否真的無法通過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

任何幫助表示贊賞,謝謝!

Google沒有提供任何記錄的API方法來通過點擊POI獲取地點ID。 但是,我能夠使用反向地理編碼獲取地點ID。

首先,我們可以通過此Stack-overflow答案中描述的方法獲取POI的坐標

其次,您可以使用getPosition()方法中的坐標來調用地理編碼方法來檢索地址組件並放置id。

這是一個工作的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> 

截至2017年12月,現在支持此功能。 以下是https://developers.google.com/maps/documentation/javascript/examples/event-poi示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM