Google Maps V3 -> Is it possible to get properties from Geojson using lat lng coordinates or other information from search box?

﹥>﹥吖頭↗ 提交于 2019-12-24 12:04:24

问题


I'm using an external geojson-file to add layers to my Google Map. This file contains two properties per area that I would like to access when searching. I can easily fetch the properties if the user clicks on an area or uses any of the other supported event handlers.

https://developers.google.com/maps/documentation/javascript/datalayer#add_event_handlers

this.map.data.addListener('mouseover', (event) => {
    console.log(event.feature.getProperty('VD'));
});

However I would like to get what geojson area I'm in when searching and from that access these properties. Is this possible to do?

searchBox.addListener('places_changed', () => {
    var places = this.searchBox.getPlaces();
    ...
    places.forEach((place) => {
    ...


回答1:


You can use containsLocation utility to check if a LatLng location is contained by a Polygon.

So in a for loop get all features in your Data layer (populated with loadGeoJson() or addGeoJson()), get each geometry and create a Polygon to use with the containsLocation() function.

For example, the following code checks if loc (LatLng) is contained in one of the features of feats array:

inWhichArea = function(map, loc) {
  for (var i in feats) {
    var area = feats[i].getGeometry();
    var poly = new google.maps.Polygon({
      paths: area.getAt(0).getArray(),
      map: map,
      clickable: false
    });
    if (google.maps.geometry.poly.containsLocation(loc, poly)) {
      return feats[i];
    }
  }
  return null;
}

Here is a jsfiddle with a working example: http://jsfiddle.net/beaver71/sgtpop4f/



来源:https://stackoverflow.com/questions/47972992/google-maps-v3-is-it-possible-to-get-properties-from-geojson-using-lat-lng-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!