Last active
December 6, 2017 17:26
-
-
Save sxwebdev/62d77bcdd188097f4747f0b8e7c613bb to your computer and use it in GitHub Desktop.
Calculate distance and speed by latitude and longitude. es6 javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calculate_ds = (lat1, lon1, lat2, lon2, timestamp1, timestamp2) => { | |
lat1 = lat1 * Math.PI / 180.0; | |
lon1 = lon1 * Math.PI / 180.0; | |
lat2 = lat2 * Math.PI / 180.0; | |
lon2 = lon2 * Math.PI / 180.0; | |
const r = 6378100; | |
const rho1 = r * Math.cos(lat1); | |
const z1 = r * Math.sin(lat1); | |
const x1 = rho1 * Math.cos(lon1); | |
const y1 = rho1 * Math.sin(lon1); | |
const rho2 = r * Math.cos(lat2); | |
const z2 = r * Math.sin(lat2); | |
const x2 = rho2 * Math.cos(lon2); | |
const y2 = rho2 * Math.sin(lon2); | |
const dot = (x1 * x2 + y1 * y2 + z1 * z2); | |
const cos_theta = dot / (r * r); | |
const theta = Math.acos(cos_theta); | |
const distance = r * theta; | |
const time_s = (timestamp2 - timestamp1); | |
const speed_mps = distance / time_s; | |
const speed_kph = (speed_mps * 3600.0) / 1000.0; | |
return { distance, time_s, speed_mps, speed_kph }; | |
} | |
calculate_ds(53.232863,44.899063,53.232858,44.898095,1512512341,1512512346) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment