Created
July 22, 2021 06:31
Star
You must be signed in to star a gist
SIT314 A2 - script.js
This file contains 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
let runCoordinates = []; | |
const startPosition = { lat: -37.93062614791752, lng: 145.4574801141134}; | |
let req = new XMLHttpRequest(); | |
req.onreadystatechange = () => { | |
if (req.readyState == XMLHttpRequest.DONE) { | |
// Pass response to function | |
convertResponseIntoArray(req.responseText); | |
} | |
}; | |
// Get run data | |
req.open("GET", "https://api.jsonbin.io/v3/b/60f903afa263d14a29799ff8/latest", true); | |
req.setRequestHeader("X-Master-Key", "$2b$10$O3vibr5wNWg3eVTsg79HYO1Cs3Jcz7rJkwA6pf5phIVhDyVLwvctq"); | |
req.send(); | |
function convertResponseIntoArray(response) { | |
// Convert response to object | |
const coordsObj = JSON.parse(response); | |
// Loop through object | |
for (var i = 0; i < coordsObj.record.length; i++) { | |
// Get current point | |
const point = coordsObj.record[i]; | |
// Add to runCoordinates | |
runCoordinates.push(point); | |
} | |
// Re-initialise map with populated array | |
initMap(); | |
} | |
function calculateDistanceInKM(point1, point2) { | |
const earthRadius = 6371.0710; // Radius of the Earth in KM | |
const lat1Rad = point1.lat * (Math.PI / 180); // Convert degrees to radians | |
const lat2Rad = point2.lat * (Math.PI / 180); // Convert degrees to radians | |
const latDifference = lat2Rad - lat1Rad; // Radian difference (latitudes) | |
const lngDifference = (point2.lng - point1.lng) * (Math.PI / 180); // Radian difference (longitudes) | |
const distance = 2 * earthRadius * Math.asin(Math.sqrt(Math.sin(latDifference / 2) * Math.sin(latDifference / 2)+Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.sin(lngDifference / 2) * Math.sin(lngDifference / 2))); | |
return distance; | |
} | |
function calculateTotalDistanceInKM() { | |
let totalDistanceInKM = 0; | |
// Loop through every point except last | |
for (let i = 0; i < runCoordinates.length - 1; i++) | |
{ | |
// Get current and subsequent points | |
const point1 = runCoordinates[i]; | |
const point2 = runCoordinates[i + 1]; | |
// Add distance between points to total distance | |
totalDistanceInKM += calculateDistanceInKM(point1, point2); | |
} | |
// Update total distance | |
document.getElementById('msg').textContent = "Total Distance: " + totalDistanceInKM.toFixed(2) + " km."; | |
} | |
function initMap() { | |
// Create map object | |
const map = new google.maps.Map(document.getElementById("map"), { | |
zoom: 15, | |
center: startPosition, | |
mapTypeId: "terrain", | |
}); | |
// Calculate total distance | |
calculateTotalDistanceInKM(); | |
// Create run path | |
const runPath = new google.maps.Polyline({ | |
path: runCoordinates, | |
geodesic: true, | |
strokeColor: "#FF0000", | |
strokeOpacity: 1.0, | |
strokeWeight: 4, | |
}); | |
// Display run path on map | |
runPath.setMap(map); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment