使用传单点在多边形中指向

Point in Polygon using leaflet-pip

本文关键字:多边形 单点      更新时间:2023-09-26

我正在尝试,给定一个包含大量点的.json,确定每个区域中有多少点(可能返回一个字典),这些点在另一个.json文件中定义。

我这样做是基于这个例子:

https://www.mapbox.com/mapbox.js/example/v1.0.0/point-in-polygon/

然而,我无法让它发挥作用。

此行:

var layer = leafletPip.pointInLayer(this.getLatLng(), states, true);

对于我的测试用例,返回为空。这是一个jsfiddle正在复制我的代码:

http://jsfiddle.net/Pe5xU/346/

map = L.map('map').setView([40.658528, -73.952551], 10);
// Load a tile layer  
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>',
  maxZoom: 18,
  minZoom: 10
}).addTo(map);
geojson = L.geoJson(data).addTo(map);
var all_markers = [];
var layers = {};
$.each(dots, function(index, rec) {
  var markers = {}
  if (rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")) {
    var marker = L.circleMarker([rec.latitude, rec.longitude], marker_style()).addTo(map);
    all_markers.push(marker);
  }
});
var all_layers = L.featureGroup(all_markers);
map.fitBounds(all_layers.getBounds());
function marker_style() {
  return {
    radius: 4,
    weight: 0,
    opacity: 1,
    color: 'white',
    dashArray: '3',
    fillOpacity: 0.7
  };
}
$.each(dots, function(index, rec) {
  if (rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")) {
    var layer = leafletPip.pointInLayer([rec.latitude, rec.longitude], geojson, true);
    console.log(layer);
  }
});

此代码示例按纬度、经度顺序提供坐标。如传单pip自述中所述,传单pip要求坐标按longitude, latitude顺序排列,与GeoJSON和其他地理空间格式相同。