[英]Google Maps Android api v2 polyline length
我正在尝试找到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));
}
我遇到过同样的问题。 这是我能够解决它的方式。
private ArrayList<LatLng> mLatLngList;
在位置更改时将位置添加到ArrayList。 compile 'com.google.maps.android:android-maps-utils:0.4'
import com.google.maps.android.SphericalUtil;
double distance = SphericalUtil.computeLength(mLatLngList);
您可以将最后一个位置的Location.distanceBetween用于当前位置。 如果您想要距离起点和终点位置的总距离,请随着位置的变化保持总计
我有同样的问题。 这是我的解决方案,其中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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.