Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Created February 19, 2020 17:20
Show Gist options
  • Select an option

  • Save ArrayIterator/ddf2e90a246fad462ddec17c1584b517 to your computer and use it in GitHub Desktop.

Select an option

Save ArrayIterator/ddf2e90a246fad462ddec17c1584b517 to your computer and use it in GitHub Desktop.
Add Bearing (Rotation Based) to Leaflet LatLng
/**
* @see https://github.com/gregallensworth/Leaflet/blob/master/LatLng_Bearings.js
*/
L.LatLng.prototype.bearingTo = function(LatLng) {
let d2r = Math.PI / 180;
let r2d = 180 / Math.PI;
let lat1 = this.lat * d2r;
let lat2 = LatLng.lat * d2r;
let dLon = (LatLng.lng-this.lng) * d2r;
let y = Math.sin(dLon) * Math.cos(lat2);
let x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
return (parseInt( Math.atan2(y, x) * r2d ) + 360 ) % 360;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment