A
A
Alexander Notfar2020-06-29 04:16:25
JavaScript
Alexander Notfar, 2020-06-29 04:16:25

How to get data (total ascent/descent) from a route - Google map API?

I parse the GPX file and get data from it about the route time and also the distance of the route. I can't find any information anywhere on how to extract "total ascent" and "total descent" data from a route. Information output example
5ef940e68458a913513576.png

My code:

jQuery.ajax({
        type: "GET",
        url: jQuery('#url_file').val(),
        dataType: "xml",
        success: function(xml) {
          var points = [];
          var bounds = new google.maps.LatLngBounds ();
          jQuery(xml).find("trkpt").each(function() {
            var lat = jQuery(this).attr("lat");
            var lon = jQuery(this).attr("lon");
            var p = new google.maps.LatLng(lat, lon);
            points.push(p);
            bounds.extend(p);
          });
          
          /*---TIME------*/
          jQuery(xml).find("wpt time").each(function() {
            	var time = jQuery(this).text();
            var b = time.split('T')[1];
            var c = b.split('Z')[0];
            var splitted = c.split(':');
            jQuery('.stats_r .time').text(splitted[0]+'H '+splitted[1]+'MIN.');
          });
          /*---------*/

          var poly = new google.maps.Polyline({
            path: points,
            strokeColor: "#01579B",
            strokeOpacity: 1,
            strokeWeight: 4
          });
          
          var startMarker = new google.maps.Marker({
            position:poly.getPath().getAt(0), 
            map:map
          });
          
          var endMarker =  new google.maps.Marker({
            position:poly.getPath().getAt(poly.getPath().getLength()-1), 
            map:map
          });
          
          
          
          /*--------DISTANCE-------*/
          google.maps.LatLng.prototype.kmTo = function(a){
            var e = Math, ra = e.PI/180;
            var b = this.lat() * ra, c = a.lat() * ra, d = b - c;
            var g = this.lng() * ra - a.lng() * ra;
            var f = 2 * e.asin(e.sqrt(e.pow(e.sin(d/2), 2) + e.cos(b) * e.cos
          (c) * e.pow(e.sin(g/2), 2)));
            return f * 6378.137;
          }

          google.maps.Polyline.prototype.inKm = function(n){
            var a = this.getPath(n), len = a.getLength(), dist = 0;
            for(var i=0; i<len-1; i++){
              dist += a.getAt(i).kmTo(a.getAt(i+1));
            }
            return dist;
          }
          jQuery('.stats_r .distance').text(poly.inKm().toFixed(2)+' KM');
          /*---------------*/					

          poly.setMap(map);

          map.fitBounds(bounds);	
          
        }
        
      });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question