Question

I'm trying to find the android maps api v2 method that will determine the length of the polyline that I am creating while I am moving. I would put it inside onLocationChanged() for continual updates. Anyone know what the method is and what units the the maps api will display the length?

Polyline line = map.addPolyline(new PolylineOptions());


public void onLocationChanged(Location location) {



line.add(new LatLng(location.getLatitude(), location.getLongitude())
 .width(5)
 .color(Color.RED));

}
Was it helpful?

Solution 2

you can use Location.distanceBetween on your last location to your current location. If you want a total distance from your start and end positions then keep a running total as your location changes

OTHER TIPS

I had the same issue. Here is how I was able to solve it.

  1. private ArrayList<LatLng> mLatLngList; Add the location to the ArrayList on Location change.
  2. Use Gradle to import this http://googlemaps.github.io/android-maps-utils/ library into your project or compile 'com.google.maps.android:android-maps-utils:0.4'
  3. Import the library import com.google.maps.android.SphericalUtil;
  4. double distance = SphericalUtil.computeLength(mLatLngList);

I had the same question. Here is my solution where points is the PolylineOptions object.

protected float calculateMiles() {
    float totalDistance = 0;

    for(int i = 1; i < points.getPoints().size(); i++) {
        Location currLocation = new Location("this");
        currLocation.setLatitude(points.getPoints().get(i).latitude);
        currLocation.setLongitude(points.getPoints().get(i).longitude);

        Location lastLocation = new Location("this");
        currLocation.setLatitude(points.getPoints().get(i-1).latitude);
        currLocation.setLongitude(points.getPoints().get(i-1).longitude);

        totalDistance += lastLocation.distanceTo(currLocation);


    }

    return totalDistance;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top