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's gravatar image

accept rate: 0%

edited 15 May '14, 13:24

aseerel4c26's gravatar image

aseerel4c26 ♦


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 ♦

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.

permanent link

answered 15 May '14, 12:55

Gys%20de%20Jongh's gravatar image

Gys de Jongh
accept rate: 17%


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

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.

permanent link

answered 16 May '14, 18:04

yvecai's gravatar image

accept rate: 9%

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.

permanent link

answered 16 May '14, 19:41

n76's gravatar image

accept rate: 17%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:


question asked: 15 May '14, 12:02

question was seen: 10,824 times

last updated: 16 May '14, 19:41

powered by OSQA