返回与Turf JS库相交的Leaflet GeoJSON多边形

时间:2019-03-19 16:09:47

标签: javascript leaflet turfjs

传单和草皮。我正在尝试在Leaflet地图上找到当用户单击地图时与点相交的所有图层。我正在使用草皮库对此进行测试。在控制台中,当我单击地图时,它将返回此错误:

var c = new L.GeoJSON.AJAX("http://127.0.0.1:8000/childcare_buff_data/",{
        style: color(c, "orange", 0.8)})
        ;
    c.addTo(map);

    map.on('click',function(e){
        lat = e.latlng.lat;
        lon = e.latlng.lng;
        ProcessClick(lat,lon)
    });

    var theMarker;
    var a;

    function ProcessClick(lat,lon){
        theMarker = L.marker([lat,lon]).addTo(map);
        c.eachLayer(function(layer) {
            intersects=turf.intersect(theMarker.toGeoJSON(),layer.toGeoJSON());
            if (intersects){
                a=layer.feature.properties.buff
                console.log(a);
                }
            })};

我收到此错误

turf.min.js:1 Uncaught TypeError: Cannot read property 'length' of null
    at turf.min.js:1
    at turf.min.js:1
    at S (turf.min.js:1)
    at Pn (turf.min.js:1)
    at Object.Lo [as intersect] (turf.min.js:1)
    at js2.js:31
    at eachLayer (leaflet.js:5)
    at ProcessClick (js2.js:30)
    at e.<anonymous> (js2.js:22)
    at e.fire (leaflet.js:5)

enter image description here

这是geojson http://www.mediafire.com/file/9fnbz32ib9n1aaj/childcare.geojson/file

更新

使用

turf.booleanWithin(theMarker.toGeoJSON(),geom.toGeoJSON());

我得到

turf.min.js:1 Uncaught Error: coordinates must only contain numbers
    at Y (turf.min.js:1)
    at Y (turf.min.js:1)
    at U (turf.min.js:1)
    at Pt (turf.min.js:1)
    at Object.Cn [as booleanWithin] (turf.min.js:1)
    at js2.js:33
    at eachLayer (leaflet.js:5)
    at ProcessClick (js2.js:31)
    at e.<anonymous> (js2.js:22)
    at e.fire (leaflet.js:5)

1 个答案:

答案 0 :(得分:0)

intersect需要两个多边形作为其参数,但是您要为其输入一个点,因此缺少length属性:

  

获取两个多边形并找到它们的交点

改为尝试turf.booleanWithin(theMarker.toGeoJSON(), layer.toGeoJSON())

  

Boolean-within如果第一个几何完全在内部,则返回true   第二种几何。