点击 POI 时获取谷歌地图上的 placeId

问题描述 投票:0回答:3

我在我的网站上使用 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&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal

感谢任何帮助,谢谢!

javascript google-maps google-maps-api-3 google-maps-markers
3个回答
4
投票

Google 没有提供任何记录的 API 方法来通过点击 POI 来获取地点 id。但是,我能够使用反向地理编码获取地点 ID。

首先,我们可以通过这个Stack-overflow回答

中描述的方法获取POI的坐标

其次,您可以使用 getPosition() 方法中的坐标来调用地理编码方法来检索地址组件和地点 id。

这是一个工作的 JS Fiddle

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>


2
投票

截至 2017 年 12 月,现已支持此功能。这是例子 https://developers.google.com/maps/documentation/javascript/examples/event-poi


0
投票
I was having the same issue, but using this reference

https://developers.google.com/maps/documentation/javascript/examples/event-poi


I tried it and it worked suprisingly. I hope you find this helpful.
Below is a angular implementation of the given functionality

1)
private map: google.maps.Map;
this.map = new google.maps.Map(document.getElementById('map'));

2) Adding a click listener on the map
this.map.addListener("click", this.handleClick.bind(this));

3) to check whether it is a mouse event, if it is retrieve place Id
public isIconMouseEvent(
    e: google.maps.MapMouseEvent | google.maps.IconMouseEvent
  ): e is google.maps.IconMouseEvent {
    return "placeId" in e;
  }

4)Below function uses isMouseEvent to get placeId and passes it to getPlaceDetails function to retrieve its details

  private handleClick(event: google.maps.MapMouseEvent | google.maps.IconMouseEvent) {
    console.log("You clicked on: " + event.latLng);
    if (this.isIconMouseEvent(event)) {
      console.log("You clicked on place:" + event.placeId);
      event.stop();
      if (event.placeId) {
        console.log(event.placeId);
        this.getPlaceDetails(event.placeId);
      }
    }
  }

5) This function is used get the place details of a place using placeService provided placeId of the place
  private getPlaceDetails(placeId: string) {
    const placesService = new google.maps.places.PlacesService(this.map);
    placesService.getDetails({ placeId }, (place, status) => {
      if (status === 'OK') {
        console.log('Place Details:', place);
      } else {
        console.error('PlacesService failed due to: ' + status);
      }
    });
  }

These may be an angular implementation, but it is more generic and easy to understand
© www.soinside.com 2019 - 2024. All rights reserved.