Java – Google Maps Android api v2 polyline length

Google Maps Android api v2 polyline length… here is a solution to the problem.

Google Maps Android api v2 polyline length

I’m trying to find the android maps api v2 method that will determine the length of the polyline I create when I move. I’ll put it in onLocationChanged() for continuous updates. Does anyone know what the method is and what units are in which the Maps API will display 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));

}

Solution

I’m having the same issue. Here’s how I solved the problem.

  1. private ArrayList<LatLng> mLatLngList;
    Add a location to the ArrayList when the location changes.
  2. Use Gradle to import this http://googlemaps.github.io/android-maps-utils/ Library to your project or
    compile 'com.google.maps.android:android-maps-utils:0.4'
  3. import

  4. library import com.google.maps.android.SphericalUtil;
  5. double distance = SphericalUtil.computeLength(mLatLngList);

Related Problems and Solutions