轨迹的暂停和重新开始
利用MovingPointOverlay smoothMarker的监听方法
smoothMarker.setMoveListener(new MovingPointOverlay.MoveListener() {
@Override
public void move(final double distance) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
index=smoothMarker.getIndex();
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
});
在监听方法中可以得到index,也就是当前所在点的下标
点击停止,smoothMarker.stopMove();
点击继续,使用index在当前下标后继续
问题:每次继续后index都是从0开始记录,这样不符合,所以我采用了子列表,把subList变为全局变量,表征剩下的路程,每次继续的时候都进行更新,来表征剩下的坐标点组成的列表。
代码:
int index=0;
List<LatLng> subList=new ArrayList<>();
public void startMove(View view) {
if (mPolyline == null) {
return;
}
// 读取轨迹点
List<LatLng> points = readLatLngs(latlngList);
// 构建 轨迹的显示区域
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(points.get(0));
builder.include(points.get(points.size() - 1));
mAMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 50));
// 取轨迹点的第一个点 作为 平滑移动的启动
LatLng drivePoint = points.get(0);
Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
points.set(pair.first, drivePoint);
subList = points.subList(pair.first, points.size());
// 设置轨迹点
smoothMarker.setPoints(subList);
// 设置平滑移动的总时间 单位 秒
smoothMarker.setTotalDuration(speed);
// 设置移动的监听事件 返回 距终点的距离 单位 米
smoothMarker.setMoveListener(new MovingPointOverlay.MoveListener() {
@Override
public void move(final double distance) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
index=smoothMarker.getIndex();
// System.out.println(smoothMarker.getIndex());
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
});
// 开始移动
smoothMarker.startSmoothMove();
//smoothMarker.stopMove();
}
public void zantingMove(View view){
smoothMarker.stopMove();
}
public void jixuMove(View view){
System.out.println(index);
List<LatLng> points = subList;
LatLng drivePoint = points.get(index);
Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(points, drivePoint);
points.set(pair.first, drivePoint);
subList = points.subList(pair.first, points.size());
// 设置轨迹点
smoothMarker.setPoints(subList);
// 设置平滑移动的总时间 单位 秒
smoothMarker.setTotalDuration(40);
// 设置移动的监听事件 返回 距终点的距离 单位 米
smoothMarker.setMoveListener(new MovingPointOverlay.MoveListener() {
@Override
public void move(final double distance) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
index=smoothMarker.getIndex();
// System.out.println(smoothMarker.getIndex());
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
});
// 开始移动
smoothMarker.startSmoothMove();
}
完美解决问题。