单次轨迹回放
已知有一段轨迹数据,点击回放按钮,小车沿着路线自动的往前运动,播放完毕也就结束了
public class MoveSingleThread extends Thread{
private List<LatLng> mLatLngList;
private Marker mCarMarker;
public MoveSingleThread(List<LatLng> latLngs, Marker marker) {
super();
mLatLngList = latLngs;
mCarMarker = marker;
}
@Override
public void run() {
super.run();
}
public void moveTrack(){
// 第一个for循环用来计算走了多少部
int step = 0;
for (int i = 0; i < mLatLngList.size() - 1; i++) {
LatLng startPoint = mLatLngList.get(i);
LatLng endPoint = mLatLngList.get(i + 1);
double slope = getSlope(startPoint, endPoint);
// 是不是正向的标示(向上设为正向)
boolean isReverse = (startPoint.latitude > endPoint.latitude);
double xMoveDistance = isReverse ? getXMoveDistance(slope) : -1 * getXMoveDistance(slope);
// 应该对经纬度同时处理
for (double j = startPoint.latitude; !((j >= endPoint.latitude) ^ isReverse); j =
j - xMoveDistance) {
step++;
}
}
// 通过距离,计算轨迹动画时间间隔
double mTimeInterval = 0;// 轨迹回放时间戳
if (!TextUtils.isEmpty(mDistance)) {
float totalDistance = Float.parseFloat(mDistance) * 1000;
if (totalDistance <= 500) {
mTimeInterval = 1000.0 / step;
} else if (totalDistance > 500 && totalDistance <= 7500) {
mTimeInterval = 2.0 * totalDistance / step;
} else {
mTimeInterval = 15000.0 / step;
}
}
// while (true) {
for (int i = 0; i < mLatLngList.size() - 1; i++) {
if (stopFlag) {
stopFlag = false;
break;
}
mIsCarMoveing = true;
LatLng startPoint = mLatLngList.get(i);
LatLng endPoint = mLatLngList.get(i + 1);
mCarMarker.setPosition(startPoint);
mCarMarker.setRotateAngle((float) getAngle(startPoint, endPoint));
double slope = getSlope(startPoint, endPoint);
// 是不是正向的标示(向上设为正向)
boolean isReverse = (startPoint.latitude > endPoint.latitude);
double intercept = getInterception(slope, startPoint);
double xMoveDistance = isReverse ? getXMoveDistance(slope) : -1 * getXMoveDistance(slope);
// 应该对经纬度同时处理
double mSleep = 0;
for (double j = startPoint.latitude; !((j >= endPoint.latitude) ^ isReverse); j =
j - xMoveDistance) {
LatLng latLng = null;
if (slope != Double.MAX_VALUE) {
latLng = new LatLng(j, (j - intercept) / slope);
// latLng = new LatLng(j, k);
} else {
latLng = new LatLng(j, startPoint.longitude);
}
mCarMarker.setPosition(latLng);
// 如果间隔时间小于1毫秒,则略过当前休眠,累加直到休眠时间到1毫秒:会损失精度
if (mTimeInterval < 1) {
mSleep += mTimeInterval;
if (mSleep >= 1) {
SystemClock.sleep((long) mSleep);
mSleep = 0;
}
} else
SystemClock.sleep((long) mTimeInterval);
}
}
}
}