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!
Solved! Go to Solution.
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
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
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!