谷歌地图Android api v2折线长度

我正在尝试找到android maps api v2方法,它将决定我在移动时创建的折线的长度。 我会把它放在onLocationChanged()中以进行持续更新。 任何人都知道方法是什么以及地图api将显示长度的单位?

Polyline line = map.addPolyline(new PolylineOptions()); public void onLocationChanged(Location location) { line.add(new LatLng(location.getLatitude(), location.getLongitude()) .width(5) .color(Color.RED)); } 

您可以将最后一个位置的Location.distanceBetween用于当前位置。 如果您想要距离起点和终点位置的总距离,请随着位置的变化保持总计

我遇到过同样的问题。 这是我能够解决它的方式。

  1. private ArrayList mLatLngList; 在位置更改时将位置添加到ArrayList。
  2. 使用Gradle将此http://googlemaps.github.io/android-maps-utils/库导入您的项目或compile 'com.google.maps.android:android-maps-utils:0.4'
  3. 导入库import com.google.maps.android.SphericalUtil;
  4. double distance = SphericalUtil.computeLength(mLatLngList);

我有同样的问题。 这是我的解决方案,其中points是PolylineOptions对象。

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