Skip to content

Instantly share code, notes, and snippets.

@bahoo
Created June 7, 2020 20:38
Show Gist options
  • Save bahoo/23db67831a5759da5ed977e9b06bfcb4 to your computer and use it in GitHub Desktop.
Save bahoo/23db67831a5759da5ed977e9b06bfcb4 to your computer and use it in GitHub Desktop.
CurvedLine.js -- curved lines in Leaflet.
// Adapted from https://medium.com/@ryancatalani/creating-consistently-curved-lines-on-leaflet-b59bc03fa9dc
// Drop-in replacement for Leaflet.curve https://github.com/elfalem/Leaflet.curve
// To auto-calculate the midpoint for you and ease code integration a bit.
// Have fun and defund the police 👍
L.CurvedLine = L.Curve.extend({
_setPath: function(path){
var start = path[0], end = path[1];
var offsetX = end['lng'] - start['lng'],
offsetY = end['lat'] - start['lat'];
var r = Math.sqrt( Math.pow(offsetX, 2) + Math.pow(offsetY, 2) ),
theta = Math.atan2(offsetY, offsetX);
var thetaOffset = (3.14/10);
var r2 = (r/2)/(Math.cos(thetaOffset)),
theta2 = theta + thetaOffset;
var midpointX = (r2 * Math.cos(theta2)) + start['lng'],
midpointY = (r2 * Math.sin(theta2)) + start['lat'];
var midpoint = [midpointY, midpointX];
this._coords = [
'M', [start['lat'], start['lng']],
'Q', midpoint,
[end['lat'], end['lng']]
];
this._bounds = this._computeBounds();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment