我在传单中有一个带有17个点(GeoJSON)的简单地图,并使用绘图工具创建了一个多边形,用于选择多边形内的点。
map.on('draw:created', function (e) { //from draw tool
var type = e.layerType,
layer = e.layer;
editableLayers.addLayer(layer);
GetSelection(editableLayers);
});
function GetSelection(layer){
var count = allPoints.getLayers().length;
console.log(count +" Sites"); //says 17
var drawList = editableLayers.getLayers().length;
console.log(drawList +" Polys"); //Says 1
if (editableLayers.getLayers().length >0){
var fcpt = turf.featurecollection(allPoints);
console.log(fcpt); // says 17
var fcpoly = turf.featurecollection(editableLayers);
console.log(fcpoly); // fails as undefined
//var ptsWithin = turf.within(fcpt,editableLayers);
var ptsWithin = turf.within(fcpt,fcpoly);
console.log(ptsWithin); //never gets this far.
};
};
有任何想法或建议吗?
答案 0 :(得分:1)
turf.featurecollection
需要一系列 GeoJSON功能,而不是像您的allPoints
和editableLayers
变量那样的传单图层组。
同样,turf.within
期望2个GeoJSON要素集合作为参数,而不是传单层组。
所以你可以直接尝试:
var ptsWithin = turf.within(allPoints.toGeoJSON(), editableLayers.toGeoJSON());
答案 1 :(得分:1)
@ghybs是对的,这是Leaflet和草皮之间的区别,而点数还可以,多边形没有过来。通过草皮GeoJson多边形信息允许它工作。
工作副本:
map.on('draw:created', function (e) {
featureGroup.clearLayers();
layer = e.layer;
featureGroup.addLayer(layer);
GetSelection(featureGroup);
});
function GetSelection(layer){
var shape2 = allPoints.toGeoJSON() //All facilities
var ptsWithin = turf.within(shape2, layer.toGeoJSON());
alert('Found ' + ptsWithin.features.length + ' features');
alert("results "+JSON.stringify(ptsWithin));
};