拖动标记并更新Turf.js缓冲区

时间:2015-04-15 16:49:27

标签: javascript leaflet turfjs

我正在尝试创建一个可以拖动标记并且缓冲区移动的地图。与question处理的内容非常相似(几乎相同),我的情况是我不使用Mapbox.js只是简单的传单。我目前的代码是:

 //add marker that is draggable
 var marker = L.marker(new L.LatLng(39.75621, -104.99404), {
                      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 = turf.featurecollection([buffered]);
     L.geoJson(buff).addTo(map);
 };

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

这给了我一个缓冲区并允许我拖动点和缓冲区,但是,之前的所有缓冲区都保留在地图中。

我做了一些更改,以便从该问题中替换mapbox.js函数,所以我的猜测是这可能是原因。

1 个答案:

答案 0 :(得分:0)

所以似乎问题出现在buff = turf.featurecollection([buffered]);,我改为buff = L.geoJson(buffered);一切正常。