Created
May 28, 2024 08:59
-
-
Save blaulan/4ce6865afc33a04bd282d93dd73dd332 to your computer and use it in GitHub Desktop.
load coordinate data from a GPX file
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
// load coordinate data from a GPX file | |
// the input will be a file uploaded by user | |
// the output will be a list of coordinates | |
function loadGPXFile(file) { | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
var xml = e.target.result; | |
var gpx = new window.DOMParser().parseFromString(xml, "text/xml"); | |
var coordinates = []; | |
var tracks = gpx.getElementsByTagName("trk"); | |
for (var i = 0; i < tracks.length; i++) { | |
var segments = tracks[i].getElementsByTagName("trkseg"); | |
for (var j = 0; j < segments.length; j++) { | |
var points = segments[j].getElementsByTagName("trkpt"); | |
for (var k = 0; k < points.length; k++) { | |
var lat = parseFloat(points[k].getAttribute("lat")); | |
var lon = parseFloat(points[k].getAttribute("lon")); | |
coordinates.push([lat, lon]); | |
} | |
} | |
} | |
console.log(coordinates); | |
} | |
reader.readAsText(file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment