mikhailshilkov / leaflet-corridor

Leaflet plugin to draw a polyline with fixed width in meters
MIT License
34 stars 9 forks source link

corridor.setLatLng #5

Open luquezsantiago86 opened 3 years ago

luquezsantiago86 commented 3 years ago

Hello, I am new in LeafletJs and this plugin is really helpful. I am trying to display a Corridor in realTime. For that, I update the coordinates using setLatLng.

My code is working with Polylines and Polygons, but when I try to use setLatLng I have an error: L.corridor.setLatLngs is not a function

Looking at the source code of leaflet-corridor, I realized that there is no function existing for this feature.

It is possible to have an aproach of how to write it myself?

luquezsantiago86 commented 3 years ago

For future readers, I could resolve this by moving and modifying the functions to the main js file, and calling directly to the polyline.setLatLngs atributte:

var lineRed = L.polyline([ [40.702222, -73.979378], [41.702222, -73.979378] ], { color: 'red' }).addTo(map); function updateWeight() { const buffer = 1 currentZoom = map.getZoom(); // getMetersPerPixel var centerLatLng = map.getCenter(); // get map center var pointC = map.latLngToContainerPoint(centerLatLng); // convert to containerpoint (pixels) var pointX = L.point(pointC.x + 10, pointC.y); // add 10 pixels to x // convert containerpoints to latlng's var latLngX = map.containerPointToLatLng(pointX); var resultado = centerLatLng.distanceTo(latLngX) / 10; // calculate distance between c and x (latitude) buffer_new = buffer * 2 / resultado return buffer_new } map.on('zoomend', function() { currentZoom = map.getZoom(); console.log('zoom:', currentZoom) if (currentZoom !== 0) { updateWeight() console.log('new buffer:', buffer_new) lineRed.setStyle({ 'weight': buffer_new }); } }); lineRed.setLatLngs(latlng)