/* * extend Leaflet's LatLng class * giving it the ability to calculate the bearing to another LatLng * Usage example: * here = map.getCenter(); / some latlng * there = L.latlng([37.7833,-122.4167]); * var whichway = here.bearingWordTo(there); * var howfar = (here.distanceTo(there) / 1609.34).toFixed(2); * alert("San Francisco is " + howfar + " miles, to the " + whichway ); * * Greg Allensworth * No license, use as you will, kudos welcome but not required, etc. */ L.LatLng.prototype.bearingTo = function(other) { var d2r = L.LatLng.DEG_TO_RAD; var r2d = L.LatLng.RAD_TO_DEG; var lat1 = this.lat * d2r; var lat2 = other.lat * d2r; var dLon = (other.lng-this.lng) * d2r; var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); var brng = Math.atan2(y, x); brng = parseInt( brng * r2d ); brng = (brng + 360) % 360; return brng; }; L.LatLng.prototype.bearingWordTo = function(other) { var bearing = this.bearingTo(other); var bearingword = ''; if (bearing >= 22 && bearing <= 67) bearingword = 'NE'; else if (bearing >= 67 && bearing <= 112) bearingword = 'E'; else if (bearing >= 112 && bearing <= 157) bearingword = 'SE'; else if (bearing >= 157 && bearing <= 202) bearingword = 'S'; else if (bearing >= 202 && bearing <= 247) bearingword = 'SW'; else if (bearing >= 247 && bearing <= 292) bearingword = 'W'; else if (bearing >= 292 && bearing <= 337) bearingword = 'NW'; else if (bearing >= 337 || bearing <= 22) bearingword = 'N'; return bearingword; };