Calculate the Traveled distance

743
2
Jump to solution
06-02-2021 05:03 AM
GuilhermeFernandes96
New Contributor

Helo!
I'm working with a route app, and I would like to know if there is any way to calculate the total distance traveled? In this example the remaining distance was calculated according to the device's location. In class TrackingStatus only the getRemainingDistance() function is presented.

If anyone has done something similar and could help me I would appreciate it.

Thank you!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RamaChintapalli
Esri Contributor

Hi,
One other way to compute the distance covered is from the Traversed Geometry returned by TrackingStatus. Something like this,

private double calculateCumulativeLengthFromPolylineGeometry(Polyline polylineGeometry) {
    ImmutablePartCollection immutablePartCollection = polylineGeometry.getParts();
    double length = 0;
    for (ImmutablePart part:
         immutablePartCollection) {
      Iterator<Segment> it = part.iterator();
      while (it.hasNext()){
        Segment segment = it.next();
        Point startPoint = segment.getStartPoint();
        Point startPointMapSpatialReference = (Point) GeometryEngine.project(startPoint,mMapView.getSpatialReference());
        Point endPoint = segment.getEndPoint();
        Point endPointMapSpatialReference = (Point) GeometryEngine.project(endPoint,mMapView.getSpatialReference());
        double segmentLength = GeometryEngine.distanceBetween(startPointMapSpatialReference,endPointMapSpatialReference);
        length = length + segmentLength;
      }
    }
    //Length returned in meters, convert to Kilometers
    return length/1000;
  }


Can get expensive if running on UI thread depending on number of segments in Geometry.

Thanks
Rama

View solution in original post

0 Kudos
2 Replies
RamaChintapalli
Esri Contributor

Hi,
One other way to compute the distance covered is from the Traversed Geometry returned by TrackingStatus. Something like this,

private double calculateCumulativeLengthFromPolylineGeometry(Polyline polylineGeometry) {
    ImmutablePartCollection immutablePartCollection = polylineGeometry.getParts();
    double length = 0;
    for (ImmutablePart part:
         immutablePartCollection) {
      Iterator<Segment> it = part.iterator();
      while (it.hasNext()){
        Segment segment = it.next();
        Point startPoint = segment.getStartPoint();
        Point startPointMapSpatialReference = (Point) GeometryEngine.project(startPoint,mMapView.getSpatialReference());
        Point endPoint = segment.getEndPoint();
        Point endPointMapSpatialReference = (Point) GeometryEngine.project(endPoint,mMapView.getSpatialReference());
        double segmentLength = GeometryEngine.distanceBetween(startPointMapSpatialReference,endPointMapSpatialReference);
        length = length + segmentLength;
      }
    }
    //Length returned in meters, convert to Kilometers
    return length/1000;
  }


Can get expensive if running on UI thread depending on number of segments in Geometry.

Thanks
Rama

0 Kudos
GuilhermeFernandes96
New Contributor

Hi Rama. Thank you so much for your answer. I've already managed to solve my problem in another way, but this method of yours seems to work very well.
Thanks!

0 Kudos