Tengo este código en el que estoy tratando de crear un búfer dinámicamente a partir de un marcador arrastrable y luego contar cuántos puntos caen dentro de este búfer. Hasta ahora puedo arrastrar el buffer sin ningún problema y la primera posición del buffer devuelve el conteo de puntos. Pero cuando arrastro el marcador el conteo no se actualiza y además el buffer termina en una capa por encima de los puntos. Este es el código:
//add marker that is draggable
var marker = L.marker(new L.LatLng(39.702475, -104.875398), {
draggable: true
});
//add marker popup
marker.bindPopup('This marker is draggable!');
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
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.geoJson(buffered);
buff.addTo(map);
console.log(buffered);
};
marker.on('drag', function () {
removeBuff(), updateBuffer()
});
updateBuffer();
//geojson call for points
var data = L.geoJson(traffacc, {
pointToLayer: function (feature, latlgn) {
var popupContect = "<b>Accident Year:</b>" + feature.properties.year_STR;
return L.circleMarker(latlgn, Style(feature)).bindPopup(popupContect);
},
}).addTo(map);
var pt = data.toGeoJSON();
var countPt = turf.count(buffered, pt, 'description');
console.log(countPt);
var resultFeatures = pt.features.concat(countPt.features);
var result = {
"type": "FeatureCollection",
"features": resultFeatures
};
Como puedes imaginar, este código es un mosaico de códigos de otras personas. ¿Alguna idea de cómo puedo lograr la segunda parte?