OL3-在每个多边形内找到任何点

时间:2018-10-29 09:42:41

标签: openlayers openlayers-3

我想找到国家中的所有点,我创建一百个点,然后在“多边形”中搜索。但未找到结果!

var count = 100;
var features = new Array(count);
var e = 4500000;
for (var i = 0; i < count; ++i) {
  var coordinates = [(2 * e * Math.random() - e)+20, (2 * e * Math.random() - e)+20];
  features[i] = new ol.Feature(new ol.geom.Point(coordinates));
}

var polyFeatures;
$.get(
  'https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json',
  function(res) {
    var format = new ol.format.GeoJSON();
    polyFeatures = format.readFeatures(res);
    polyFeatures.forEach(function(poly) {
      features.forEach(function(point) {
        //console.log(poly.getGeometry().getExtent())
        if(poly.getGeometry().intersectsExtent(point.getGeometry().getExtent())){
          console.log('Fount!');
        }else
        {console.log('Not Fount!')}
      });
    });
  }
);

Plunker上的代码为here

1 个答案:

答案 0 :(得分:2)

您的随机点在地图坐标中,geojson在lonlat中,您需要在搜索中转换其中之一

if(poly.getGeometry().clone().transform('EPSG:4326','EPSG:3857').intersectsExtent(point.getGeometry().getExtent())){

if(poly.getGeometry().intersectsExtent(point.getGeometry().clone().transform('EPSG:3857','EPSG:4326',).getExtent())){