Turf.buffer 和一个可拖动的标记

Turf.buffer and a draggable marker

到目前为止,我已经创建了一个标记,将其传输到 geoJSON,并使用 Turf.buffer 在其周围创建了一个缓冲区。当我在地图上拖动它时,如何将此缓冲区 "stick" 连接到标记?

   <script>
        L.mapbox.accessToken = 'fg.eyJ1IjoisdflksdaklsjZWwiLCJhIjoiRHNwX0ZWNCJ9.Ov2O5sslkdqV93_R0lq3Q';
        var map = L.mapbox.map('map', 'example.kf6j9ec4')
            .setView([38.633, -90.319],12);

        var marker = L.marker(new L.LatLng(38.633, -90.319), {
            icon: L.mapbox.marker.icon({
                'marker-color': '1B05E3', 
                "marker-symbol": "pitch"
            }),
            draggable: true
        });

        marker.bindPopup('This marker is draggable! Move it around to see what locales are in your "area of walkability".');

        //Make the marker a feature
        var pointMarker = marker.toGeoJSON();

        //buffer the marker geoJSON feature
        var buffered = turf.buffer(pointMarker, 2, 'miles');

        var resultFeatures = buffered.features.concat(pointMarker);
        var result = {
            "type": "FeatureCollection",
            "features": resultFeatures
        };

        L.mapbox.featureLayer().setGeoJSON(buffered).addTo(map);
        marker.addTo(map);

    </script>

首先,我不得不说,我对 Turf.js 一点都不熟悉,所以如果我错了,请开枪打死我。关于您在做什么我不明白的一件事是为什么要将 buffered 添加到 featureLayer 而不是 result 对象?我想你打算这样做,所以我在下面的例子中改变了它。

您需要连接到标记的 dragend 事件。在那里,您需要根据标记的当前 geojson 对象获取新结果。所以你最好写一个函数来获取结果,这样你就可以在第一次放置标记和每次拖动标记时使用它。在代码中:

// Add empty featureLayer
var layer = L.mapbox.featureLayer().addTo(map);

var marker = L.marker(new L.LatLng(38.633, -90.319), {
    draggable: true
});

// Attach handler on dragend event
marker.on('dragend', function (event) {
    // Get new results based on marker's current geojson
    var results = getResults(event.target.toGeoJSON());
    // Add the results to the featurelayer
    layer.setGeoJSON(results);
});

marker.addTo(map);

// Function for getting results
function getResults (geojson) {
    // You returned buffered, makes no sense to me
    // Changed it to return the new featurecollection
    // But you can alter it to what you need
    var buffered = turf.buffer(geojson, 2, 'miles'),
        resultFeatures = buffered.features.concat(geojson);
    return  {
        "type": "FeatureCollection",
        "features": resultFeatures
    };
}

// Get results the first time
var results = getResults(marker.toGeoJSON());
// Add the results to the featurelayer
layer.setGeoJSON(results);

现在您将在每次拖动标记时获得更新的结果。这段代码是不是又快又脏而且无法测试它,因为我不知道如何使用 turf.否则我会在 Plunker 上创建一个示例。如果您 运行 遇到任何问题,请告诉我。希望对你有帮助,祝你好运!

因此,借助上述代码和大量谷歌搜索,我想出了一个可行的解决方案。什么有效:添加一个可拖动标记,然后使用 "marker.on" 方法启动一个函数来清除所有旧缓冲区,然后一个函数在当前位置周围重新绘制缓冲区。

        //add marker that is draggable
        var marker = L.marker(new L.LatLng(38.633, -90.319), {
            icon: L.mapbox.marker.icon({
                'marker-color': '1B05E3', 
                "marker-symbol": "pitch"
            }),
            draggable: true
        });

        //add marker popup
        marker.bindPopup('This marker is draggable! Move it around to see what locales are in your "area of walkability".');
        marker.addTo(map);

        //remove old buffers (used when marker is dragged)
        function removeBuff(){
            map.removeLayer(buff);
            };

        //create buffer (used when the marker is dragged)
        function updateBuffer(){
            //Make the marker a feature
            var pointMarker = marker.toGeoJSON();
            //buffer the marker geoJSON feature
            var buffered = turf.buffer(pointMarker, 1, 'miles');
            //add buffer to the map. Note: no "var" before "buff" makes it a global variable and usable within the removeBuff() function. 
            buff = L.mapbox.featureLayer(buffered);
            buff.addTo(map);
        };

        marker.on('drag', function(){removeBuff(), updateBuffer()});
        updateBuffer();