After drawing a polyline, I would like to draw a circle from the center of this line whose radius encompasses the entire line.
For polyline's that have more than 2 points I can crudely get the center point like this:
// Get middle latLng on polyline var streetLatLngs = layer.getLatLngs(); var numPoints = streetLatLngs.length; var middleIndex = Math.round(numPoints / 2); var centerPoint = streetLatLngs[middleIndex]; var circle = L.circle(centerPoint, 100); circle.addTo(map);
However as you can see when there are only 2 points, I can't calculate the center latlng, and I can't guarantee that the middleIndex is actually the middle point.
Once I have that I can get the radius with:
Math.round(streetLatLngs.distanceTo(streetLatLngs[numPoints - 1]) / 2);
which correctly produces the circle raduis: