单次轨迹回放

已知有一段轨迹数据,点击回放按钮,小车沿着路线自动的往前运动,播放完毕也就结束了

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

转载于:https://www.cnblogs.com/ly570/p/11369960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值