Skip to content

Instantly share code, notes, and snippets.

@blaulan
Created May 28, 2024 08:59
Show Gist options
  • Save blaulan/4ce6865afc33a04bd282d93dd73dd332 to your computer and use it in GitHub Desktop.
Save blaulan/4ce6865afc33a04bd282d93dd73dd332 to your computer and use it in GitHub Desktop.
load coordinate data from a GPX file
// 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