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
Google Maps Android api v2 polyline length
-
29-09-2022 - |
Вопрос
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));
}
Решение 2
Другие советы
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.- 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'
- Import the library
import com.google.maps.android.SphericalUtil;
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;
}
Не связан с StackOverflow