3

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));

}
johnsonjp34
  • 3,139
  • 5
  • 22
  • 48
  • do you want to draw your path when device move ? – mohammed momn Jan 17 '14 at 22:20
  • @mohammedmomn I just need the length of the polyline that's created as I move. Yes I want it to draw the path, but more importantly I need the path length. Thanks – johnsonjp34 Jan 17 '14 at 22:21
  • Maybe [this](http://stackoverflow.com/questions/19479667/how-can-i-calculate-a-current-distance-to-polyline-points-on-google-maps-v2-in-a) can help you – Alejandro Cumpa Jan 17 '14 at 22:28

3 Answers3

11

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);
Solomon Ayoola
  • 2,639
  • 3
  • 18
  • 21
4

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

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • This is what I did not want to do. Imagine a case where my car is on a no network zone for a while. The whole calculation will go wrong. Isnt there a direct api which takes a list of points and then give a distance – Utsav Gupta Mar 28 '16 at 06:48
  • Why does it matter if you have an internet connection or not `distanceBetween` does not make a network call – tyczj Mar 28 '16 at 19:40
2

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;
}
MikeSchem
  • 950
  • 2
  • 16
  • 29
  • and where is `.distanceTo` defined? – kosemuckel Apr 12 '17 at 08:08
  • It's defined in the location object https://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location) – MikeSchem Apr 12 '17 at 14:59
  • You need to multiply it with EARTH_RADIUS . check computeLength method https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/SphericalUtil.java – Omar Beshary Aug 22 '19 at 10:54