目前需要做这样一个需求,后台返回一堆打点数据,要求Android端实现轨迹回放功能,带动画的那种,还需要进度条,支持前进后退,先上图,看是否是你需要的功能。
不多说直接上代码,目前的经纬度先使用类写死
TrackRecordActivity.class
public class TrackRecordActivity extends BaseFatherFragmentActivity implements
BaiduMap.OnMapLoadedCallback,
BaiduMap.OnMapStatusChangeListener,
BaiduMap.OnMarkerClickListener,
BaiduMap.OnMapClickListener {
@BindView(R.id.tvTextTitle)
TextView tvTextTitle;
@BindView(R.id.btnTitleBack)
ImageButton btnTitleBack;
@BindView(R.id.baiduMap)
MapView baiduMapView;
@BindView(R.id.layoutBaidu)
FrameLayout layoutBaidu;
@BindView(R.id.view_control)
View view_control;
@BindView(R.id.iv_forward)
ImageView iv_forward;
@BindView(R.id.iv_play)
ImageView iv_play;
@BindView(R.id.iv_next)
ImageView iv_next;
@BindView(R.id.seekbar_play)
SeekBar seekbar_play;
@BindView(R.id.tv_mark_time)
TextView tv_mark_time;
private int playProgress;
private BaiduMap baiduMap;
private MyLocationListenner myListener = new MyLocationListenner();
private LocationClient mLocClient;
private BDLocation myLocation;
private int mapFitZoom = 10;
private String mDevCode;
private String trackRecodeId;
private List<Marker> markers;
private boolean isPlayTrack = false;
private LatLng playPosition = null;
private List<HistoryLocationBean> historyLocationBeans = new ArrayList<>();
private List<LatLng> routeList = new ArrayList<>();// 路线点的集合
private int routeIndex;
private boolean routeFlag;
private final int ROUTE = 0;
private Marker routeMarker;
private int ROUTETIME = 500;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == ROUTE) {
mHandler.sendEmptyMessage(PROGRESS);
if (routeIndex == routeList.size() - 1) {
isPlayTrack = false;
routeFlag = false;
iv_play.setImageResource(R.drawable.ic_player_start);
AppLog.Loge("播放完毕");
routeIndex = 0;
if (routeMarker != null) {
routeMarker.remove();
routeMarker = null;
}
// addRouteLine(routeList);
//画路线
if (routeList.size() > 0) {
addMarker(routeList);
}
// drawMyRoute(routeList);
return;
}
if (routeIndex != 0) {
isPlayTrack = true;
//画路线
drawMyRoute(routeList.subList(0,
routeIndex+1));
//描点
playPosition = routeList.get(routeList.size()-1);
drawMapMarkView();
// //描起点
needAddMapMark(routeList.get(0),0);
}
// 页面跟随移动,注掉这行就是在原图上绘制
// moveToLocation(routeList.get(routeIndex), false);
if (routeMarker == null) {
Bitmap bitmap1= BitmapFactory.decodeResource(getResources(),R.drawable.track_run);
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(AppUtils.imageScale(bitmap1,AppUtils.dp2Px(TrackRecordActivity.this,27),
AppUtils.dp2Px(TrackRecordActivity.this,38)));
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(routeList.get(routeIndex++))
.icon(bitmap);
//在地图上添加Marker,并显示
routeMarker = (Marker) baiduMap.addOverlay(option);
} else {
routeMarker.setPosition(routeList.get(routeIndex++));
}
routeMarker.setToTop();
routeMarker.setDraggable(false);//设置 marker 是否允许拖拽,默认不可拖拽
routeMarker.setPerspective(false);//设置是否开启 Marker 覆盖物近大远小效果,默认开启
// setUserMapCenter(routeList.get(routeIndex), mapFitZoom);
mHandler.sendEmptyMessageDelayed(ROUTE, ROUTETIME);
}
if (msg.what == PROGRESS) {
if (routeIndex == 0) {// 因为播放完毕时routeIndex被赋值成了0,不写进度条会直接跳到0的位置
setProgss(100);
} else {
setProgss((routeIndex + 1) * 100 / routeList.size());
}
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_track_record);
ButterKnife.bind(this);
initVariable();
setViews();
setClick();
initSeekBar();
updatePlayMarkUi(false);
getData();
}
@Override
protected void onStart() {
super.onStart();
if (!mLocClient.isStarted()) {
mLocClient.start();
}
}
@Override
protected void onStop() {
super.onStop();
mLocClient.stop();
}
private void initSeekBar() {
seekbar_play.setMax(100);
seekbar_play.setProgress(0);
playProgress = seekbar_play.getProgress();
seekbar_play.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
playProgress = progress ;
AppLog.Loge("移动的进度为:"+progress);
if (!isPlayTrack){//播放过程中无效
int selectIndex = 0;
if(routeList!=null && routeList.size()>0){
float seachPoi = (float) ((progress*routeList.size())/100.0);
if((seachPoi-(int)seachPoi)>0.5){
selectIndex = (int)seachPoi+1;
}else{
selectIndex = (int)seachPoi;
}
//防止溢出
if(selectIndex >= routeList.size()-1){
selectIndex = routeList.size()-1;
}
}
routeIndex = selectIndex;
AppLog.Loge("实际选择的下标为:"+routeIndex);
//描点
playPosition = routeList.get(selectIndex);
drawMapMarkView();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void initVariable() {
markers = new ArrayList<>();
Intent intent