我试图给定一个包含很多点的 .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 © <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);
}
});
最佳答案
此代码示例提供按纬度、经度顺序排列的坐标。如documented in the leaflet-pip readme ,leaflet-pip 期望坐标按经度、纬度
顺序排列,与 GeoJSON 和其他地理空间格式相同。
关于javascript - 使用 leaflet-pip 绘制多边形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187944/