31

This is my coding I used in Leaflet:

    var pointA = new L.LatLng(28.635308, 77.22496);
    var pointB = new L.LatLng(28.984461, 77.70641);
    var pointList = [pointA, pointB];

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

    });

    map.addLayer(firstpolyline);

The map is not showing any result. I just need straight line from one point to another, just visualize it, not geodetic correct. Points (in Leaflet "circles") are showing perfectly on the map.

5 Answers 5

50

Is your code pasted directly? If so,

var pointA = new L.LatLng(28.635308, 77.22496);
var pointB = new L.LatLng(28.984461, 77.70641);
var pointList = [pointA, pointB];

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

});

has missing comma's on lines 5 & 8, and line 12 use firstpolyline.addTo(map). Make it

var pointA = new L.LatLng(28.635308, 77.22496);
var pointB = new L.LatLng(28.984461, 77.70641);
var pointList = [pointA, pointB];

var firstpolyline = new L.Polyline(pointList, {
    color: 'red',
    weight: 3,
    opacity: 0.5,
    smoothFactor: 1
});
firstpolyline.addTo(map);
2
  • I can see only one error, and I fixed it. But polyline is still not showing on my map. What am I doing wrong? Commented Jul 19, 2012 at 9:17
  • You just missed a small line of code at the end. The polyline was correctly coded, but it was not added to the map as Lealflet requires: firstpolyline.addTo(map);
    – user10056
    Commented Sep 7, 2012 at 16:45
6

I used following code to draw polyline between various Locations:

var polylinePoints = [
    [37.781814, -122.404740],
    [37.781719, -122.404637],
    [37.781489, -122.404949],
    [37.780704, -122.403945],
    [37.780012, -122.404827]
  ];            
  
  var polyline = L.polyline(polylinePoints).addTo(map);

For more Details you can follow this Link.

3

addLayer doesn't work for me, had to do .addTo(map)

1

Use lower case "polyline" in L.polyline(...). This will work.

-1

If you want exact path(curve shapes), use polyline.antPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.