perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

Export route as Geojson and add it as a layer #625

Open Nikelsj opened 3 years ago

Nikelsj commented 3 years ago

I want to export the route between the waypoints as geojson so that i can buffer and add it as a line layer Here is my code :

`var route = L.Routing.control({ waypoints: [null], collapsible: true, show: false, router: new L.Routing.graphHopper('key'), position: 'bottomleft', geocoder: L.Control.Geocoder.nominatim({}), createMarker: function(i,wp) { return L.marker(wp.latLng, {icon: redIcon, draggable:true }); }, }).addTo(map);

route.on('routeselected', function(e) { var r = e.route; var lineCoordinates = [], i, latLng; for (i = 0; i < r.coordinates.length; i++) { latLng = L.latLng(r.coordinates[i]); lineCoordinates.push([latLng.lng, latLng.lat]); } return { type: 'LineString', coordinates: lineCoordinates } let lineString = turf.lineString(lineCoordinates); var buffered = turf.buffer(lineString, 500, {units: 'miles'}); var bufferedLayer = L.geoJSON(null); bufferedLayer.addData(buffered); bufferedLayer.addTo(map); });`

I want to know if I am in the right way? i am not getting any coordinates from the route