Skip to content

Commit

Permalink
Get location & walking time
Browse files Browse the repository at this point in the history
  • Loading branch information
vardhanapoorv committed Feb 28, 2020
1 parent 66d4a74 commit de04d1d
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion train-app/companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4342,4 +4342,39 @@ const StationsIDMapping = [
origin: "Grange Hill Underground Station",
towards: ["Hainault", "Woodford Via Hainault"]
}
];
];

const ablyAPIKey = '<YOUR-API-KEY>';
var realtime = new Ably.Realtime(ablyAPIKey);
const apiKey = '<YOUR-GOOGLE-API-KEY>';
let watchID = geolocation.watchPosition(locationSuccess, locationError);
let latitude, longitude, timeToArrival, walkingTime;

function locationSuccess(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
if (JSON.parse(settingsStorage.getItem("line")) === null) return;
// Reading the origin station from Companion Settings
const station = JSON.parse(settingsStorage.getItem("origin")).name;
if (station === "") return;

// Replace the space with plus so that it can be passed to Google API
let dest = station.split(" ").join("+");
let googleUrl = `https://maps.googleapis.com/maps/api/distancematrix/json?origins=${latitude},${longitude}&destinations=${dest}&mode=walking&key=${apiKey}`;
fetch(googleUrl, {
method: "GET"
})
.then(function(res) {
return res.json();
})
.then(function(data) {
let myData = data;
// Convert the time to mins
walkingTime = Math.floor(myData.rows[0].elements[0].duration.value / 60);
})
.catch(err => console.log("[FETCH]: " + err));
}

function locationError(error) {
console.log("Error: " + error.code, "Message: " + error.message);
}

0 comments on commit de04d1d

Please sign in to comment.