0

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

   <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>
4

2 回答 2

0

首先,我不得不说,我对 Turf.js 一点也不熟悉,所以如果我错了就开枪吧。我不明白你在做什么的一件事是你为什么要添加bufferedfeatureLayer不是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);

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

于 2015-03-05T00:22:50.847 回答
0

因此,在上面的代码和大量谷歌搜索的帮助下,我想出了一个可行的解决方案。什么工作:添加一个可拖动的标记,然后使用“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();
于 2015-03-12T18:50:21.587 回答