Skip to content

Instantly share code, notes, and snippets.

@SteveUpton
Last active March 4, 2016 19:10
Show Gist options
  • Save SteveUpton/ba89942506354d23fad8 to your computer and use it in GitHub Desktop.
Save SteveUpton/ba89942506354d23fad8 to your computer and use it in GitHub Desktop.
Given LocationHistory.json exported from Google Location History, calculates total distance traveled and estimates how much was walked.
results = require('./LocationHistory.json')
.locations
.map(function (location) {
return {
time: location.timestampMs,
lat: location.latitudeE7 / 10000000,
long: location.longitudeE7 / 10000000,
}
})
.reduce(function (results, curr, index, array) {
if (index === array.length - 1) { return results };
deltaDistance = getDistance(curr.lat,
curr.long,
array[index+1].lat,
array[index+1].long);
deltaTime = (curr.time - array[index+1].time)/1000;
speed = deltaDistance / deltaTime;
// 1.3 m/s (average walking speed)
if (speed > 1.3) {
results.vehicleDistance += deltaDistance;
} else {
results.walkingDistance += deltaDistance;
}
results.totalDistance += deltaDistance;
return results;
}, {
totalDistance : 0,
walkingDistance : 0,
vehicleDistance : 0,
});
// Distance between lat long points in m
// From http://stackoverflow.com/a/21623206
function getDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295; // Math.PI / 180
var c = Math.cos;
var a = 0.5 - c((lat2 - lat1) * p)/2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p))/2;
// 2 * R; R = 6371 km
return 12742 * Math.asin(Math.sqrt(a)) * 1000;
}
console.log(`Total distance travelled: ${ results.totalDistance/1000 } km`);
console.log(`Distance travelled by vehicle: ${results.vehicleDistance/1000} km`);
console.log(`Distance walked: ${results.walkingDistance/1000} km`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment