Search code examples
leafletopenstreetmap

How to draw a path between two nodes using Leaflet


I have a set of lat and long points which form a route from source to destination. I have used polyline method of Leaflet to draw the path between the source to destination, but it gives a scrambled path.

      var firstpolyline = new L.polyline(latlong, {
      color: 'red',
      weight: 3,
      opacity: 0.5,
      smoothFactor: 1

      });
      firstpolyline.addTo(mym[![enter image description here][1]][1]ap);

The latlong in the above code is an array of latitude and longitude points. But it gives a scrambled output like this:

polyline forming scrambled outputimgur.com/aZrGa.jpg

But the latlong points form a single correct path from source to destination. I have been using polyLine. What mistake am I doing? Should I use some other methods of Leaflet?

Edit after @ivansanchez comment

The latlong arrays are of type L.LatLng(x,y) where L is the Leaflet object. Here is a snippet:

enter image description here


Solution

  • It was my mistake, Polyline works properly. I had an array of latlng that were not in an order. Putting an ordered latlng points helped me plot the route correctly between source and destination.