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

时间:2015-03-04 23:37:36

标签: javascript leaflet mapbox geojson turfjs

到目前为止,我已经创建了一个标记,将其转换为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>

2 个答案:

答案 0 :(得分:0)

预付款,我必须说,我根本不熟悉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);

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

答案 1 :(得分: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();