将containsLocation与google.maps.Data.polygon一起使用

cam*_*enl 4 google-maps google-maps-api-3

我有一个带有面要素的google.maps.Data图层:

state = new google.maps.Data();
state.loadGeoJson('static/geojson/ga_state.geojson', {
    idPropertyName: 'name'
});
state.setStyle({
    clickable: false,
    visible: false,
});
state.setMap(map);
Run Code Online (Sandbox Code Playgroud)

在此要素集内是一个表示格鲁吉亚州的多边形:

ga = state.getFeatureById('Georgia')
Run Code Online (Sandbox Code Playgroud)

我可以得到这个功能的几何:

gaGeom = ga.getGeometry()
Run Code Online (Sandbox Code Playgroud)

但是当我将这些对象中的任何一个以及原始数组传递给google.maps.geometry.polygon.containsFeature()时,我得到一个错误,该对象不包含get()函数:

google.maps.geometry.poly.containsLocation(p.getPosition(), ga)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom.getArray())
Uncaught TypeError: b.get is not a function(…)
Run Code Online (Sandbox Code Playgroud)

如何将google.maps.Data.Polygon转换为google.maps.Polygon或使用此功能?

编辑:

我找到了一种从google.maps.Data.Polygon构建google.maps.Polygon的方法:

//gaGeom is the feature.geometry from the data layer
poly = new google.maps.Polygon({paths:gaGeom.getAt(0).getArray()})
Run Code Online (Sandbox Code Playgroud)

但肯定有一种更清洁的方式来构建google.maps.Polygon?

geo*_*zip 5

google.maps.geometry.poly.containsLocation方法需要一个点(一个google.maps.LatLng)和多边形(一个google.maps.Polygon).

containsLocation(point:LatLng,polygon:Polygon)
返回值:boolean计算给定点是否位于指定的多边形内.

  • ga = state.getFeatureById('Georgia') 返回"功能"
  • gaGeom = ga.getGeometry()返回"几何"
  • gaGeom.getArray()返回LinearRings 的"数组"

这些都不是google.maps.Polygon.你可以从数组中创建一个google.maps.Polygon(正如我在写这篇文章时看到的那样).

概念证明

码:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var state = new google.maps.Data();
  var poly;
  state.addListener('addfeature', function(evt) {
    if (evt.feature.getId() == "Georgia") {
      var ga = state.getFeatureById('Georgia');
      var gaGeom = ga.getGeometry();
      //gaGeom is the feature.geometry from the data layer
      poly = new google.maps.Polygon({
        paths: gaGeom.getAt(0).getArray(),
        map: map,
        clickable: false
      });
    }
  });
  var infoWindow = new google.maps.InfoWindow();
  google.maps.event.addListener(map, 'click', function(evt) {
    infoWindow.setPosition(evt.latLng);
    if (google.maps.geometry.poly.containsLocation(evt.latLng, poly)) {
      infoWindow.setContent("INSIDE POLY<br>" + evt.latLng.toUrlValue(6));
    } else {
      infoWindow.setContent("OUTSIDE POLY<br>" + evt.latLng.toUrlValue(6));
    }
    infoWindow.open(map);
  });

  state.loadGeoJson("http://www.geocodezip.com/GeoJSON/gz_2010_us_040_00_500k.json.txt", {
    idPropertyName: 'NAME'
  });
  state.setStyle({
    clickable: false,
    visible: false,
  });
  state.setMap(map);

  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': "State of Georgia"
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      map.fitBounds(results[0].geometry.viewport);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
Run Code Online (Sandbox Code Playgroud)