NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

# Get distance from gpx programmatic-ally using javascript

 0 I have a gpx file. I mapped the route to a local openstreetmap (followed https://wiki.openstreetmap.org/wiki/Openlayers_Track_example example). My requirement is to get distance of the route. How can i achieve that? I need to achieve it programmatic-ally using javascript. Please help, Thanks. asked 15 May '14, 12:02 ErmaIsabel 1●1●1●1 accept rate: 0% aseerel4c26 ♦ 32.6k●18●248●554 1 What about an external solution (as gpsPrune for example), or does it need to be a webimplementation? (15 May '14, 12:45) iii So there is no relation to OSM at all, right? It is just how to calculate the distance from a "route" inside a gpx file? OSM is just the background for viewing, right? (15 May '14, 13:26) aseerel4c26 ♦

 0 Hi Ermalsabel, the short answer : 1) Get a (free) copy of BaseCamp 2) Import your *.gpx file in BaseCamp 3) Right Click the file 4) Choose open. One of the statistics is the total lenght The long answer : 1) Get a (free) copy of eg GpsBabel 2) Convert the .gpx to .csv 3) Use a spreadsheet to calculate any statistic I use Excel but others will work. Calculating distances can be done with The haversine formula but also more straight forward with the spherical law of cosines if you use double precision variables. Also use double precision to calculate time and average speed if there are a few thousand data points in your *.Gpx log as adding increase the error proportionally. answered 15 May '14, 12:55 Gys de Jongh 713●1●5●14 accept rate: 16% 3 Thank you very much for your reply.I need to achieve it programmatic-ally using javascript. Sorry my question doesn't explain much (15 May '14, 13:08) ErmaIsabel
 0 Openlayers is a JavaScript lib that allows to show maps on websites. It also contains stuff to handle gpx data, and a few geometric functions that would very probably answer your need. answered 16 May '14, 18:04 yvecai 1.5k●12●26 accept rate: 9%
 0 GPX files are XML. Its been a while since I've played with Javascript so I can't remember if the XML calls I was using are built in or were part of a library. But anyway, your first step is parsing the GPX to get an array of data. You may find a bunch of "wpt" (waypoint) elements which you want to ignore. Then there can be one or more "trk" (track) elements that you are interested in. Each track element contains a bunch of "trkpt" elements with "lat" and "lon" attributes. You want to add the distances between each track element to get the total length of the track. However you don't want the angular distance summed: Convert it to feet/meters or what ever units you need first. There are some quick and dirty conversions you could do but you'll probably want to read up on the official equations for converting spherical coordinates to linear distances. Since that is just math, you can do it in Javascript. answered 16 May '14, 19:41 n76 10.8k●10●82●172 accept rate: 17%

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

×255
×63
×60
×9

question asked: 15 May '14, 12:02

question was seen: 11,598 times

last updated: 16 May '14, 19:41

### Related questions

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum