Google Maps Android api v2 polyline length

懵懂的女人 提交于 2019-12-05 21:14:36

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!