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));
}
I had the same issue. Here is how I was able to solve it.
private ArrayList<LatLng> mLatLngList;
Add the location to the ArrayList on Location change. compile 'com.google.maps.android:android-maps-utils:0.4'
import com.google.maps.android.SphericalUtil;
double distance = SphericalUtil.computeLength(mLatLngList);
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 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.