我正在尝试创建一个地图,其中一个标记是可拖动的,并且它可以缓冲移动。与这个question处理的内容非常相似(几乎相同),但在我的例子中,我使用的不是Mapbox.js,而是普通的leaflet。我当前的代码是:
//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函数,所以我猜这可能是原因。
发布于 2015-04-16 01:32:44
所以看起来问题出在buff = turf.featurecollection([buffered]); one中,我改成了buff = L.geoJson(buffered); everything works。
https://stackoverflow.com/questions/29656147
复制相似问题