使用leaflet-pip指向Polygon

时间:2015-12-09 20:01:53

标签: javascript leaflet mapbox point-in-polygon

我试图在给出包含大量点数的 .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);
  }
});

1 个答案:

答案 0 :(得分:1)

此代码示例提供纬度,经度顺序的坐标。作为documented in the leaflet-pip readme,leaflet-pip需要longitude, latitude顺序的坐标,与GeoJSON和其他地理空间格式相同。