javascript - 多边形内的 Leaflet 和 Turf.js 点

标签 javascript leaflet turfjs

我在传单中有一个包含 17 个点 (GeoJSON) 的简单 map ,并使用绘图工具创建一个多边形以用于选择多边形内的点。

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.

    };
};

有什么想法或建议吗?

最佳答案

turf.featurecollection需要一组GeoJSON功能,而不是像allPointseditableLayers变量那样的Leaflet Layer Group。

同样,turf.within需要 2 个 GeoJSON 要素集合作为参数,而不是 Leaflet 图层组。

所以你可以直接尝试:

var ptsWithin = turf.within(allPoints.toGeoJSON(), editableLayers.toGeoJSON());

关于javascript - 多边形内的 Leaflet 和 Turf.js 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36019221/

相关文章:

javascript - JS 从字符串中提取指数

javascript - 图表 : how to show only max and min values on y-axis

javascript - 停止在 innerHTML 插入时清除输入

javascript - 单击 chart.js 中的各个栏时如何显示 Bootstrap 模式

javascript - 使用传单标记在单击事件时添加文本到文档

leaflet - 查找 LineString 与 turfjs 中的多边形边界相交的坐标

javascript - 草皮.js : Buffer executing but geojson not being added to leaflet map

javascript - 单击传单标记会将您带到 URL

leaflet - 使用 Leaflet.js,如何遍历集群中的标记?

javascript - 如何在mapbox中获取圆的wkt