首页 > 代码库 > 安卓实现在百度地图上播放历史轨迹

安卓实现在百度地图上播放历史轨迹

 把一个人一个月的轨迹历史描绘出来,并实现按时间顺序回放。小弟菜鸟一枚,代码质量不高,高手勿喷,谢谢。

关于描点和描线,百度地图都有自己的API,大家都应该会了;至于播放动画,肯定不止一种方法,如果大家有好的方法,分享出来,小弟感激不尽。
我的方法是:在一个线程里,开始播放后,遍历存放item的list,找到当前的item,移除他的marker,然后给他换一个marker,再添加到list里面,然后睡眠;接着就判断,如果不是当前item,该什么样子就什么样子,如此迭代就可以了。
代码如下:

/** 轨迹回放逻辑处理 **/
private Runnable rz = new Runnable()
{
  @Override
  public synchronized void run()
  {
   while (isStartRun)
   {
    for (int i = index; i < mGeoList.size(); i++)
    {
     OverlayItem item = mGeoList.get(i);
     flag = index;
     sendMsg(MapApplication.LOCATEOVER_NOTICE_ADAPTER);
     mOverlayGraphic.removeItem(item);
     item.setMarker(res.get(3));
     if (!isBcak)
     {
      break;
     }
     mOverlayGraphic.addItem(item);
     mMapView.refresh();
     SystemUtils.sleep(1000);
     mOverlayGraphic.removeItem(item);
     if (index == 0)
     {
      item.setMarker(res.get(1));
     }
     else if (index == mGeoList.size() - 1)
     {
      item.setMarker(res.get(2));
      isStartRun = false;
      isFinish = false;
     }
     else
     {
      View view = mInflater.inflate(R.layout.overlay_item, null);
      TextView userItemTv1 = (TextView) view.findViewById(R.id.item_tv1);
      userItemTv1.setText(index + 1 + "");
      Bitmap bmp = userLocateUtil.createNewBitmap(view);
      Drawable drawable = new BitmapDrawable(bmp);
      item.setMarker(drawable);
     }
     if (!isBcak)
     {
      break;
     }
     mOverlayGraphic.addItem(item);
     mMapView.refresh();
     index++;
     if (!isFinish)
     {
      index = 0;
      sendMsg(MapApplication.LOCATEOVER_PLAY_FINISH);
      break;
     }
    }
   }
  }
};
 
private Handler handler = new Handler()
{
  public void handleMessage(Message msg)
  {
   switch (msg.what)
   {
    case MapApplication.LOCATEOVER_CHANGE_MAPVIEW:
     changeMapView();
     break;
    case MapApplication.LOCATE_MAPVIEW_REFRESH:
     mMapView.refresh();
     mMapView.getController().zoomToSpan(mOverlayGraphic.getLatSpanE6(), mOverlayGraphic.getLonSpanE6());
     mMapView.getController().setCenter(mOverlayGraphic.getCenter());
     PocProgressDialog.cancel();
     break;
    case MapApplication.LOCATEOVER_PLAY_FINISH:
     Toast.makeText(MainActivity.mainActivity, "播放完毕", Toast.LENGTH_SHORT).show();
     flag = 99999;
     adapter.notifyDataSetChanged();
     play.setText("开始回放");
     break;
    case MapApplication.LOCATEOVER_NOTICE_ADAPTER:
     adapter.notifyDataSetChanged();
     // mListView.setSelection(flag);备用
     break;
   }
  }
};

 

主要的逻辑就在这里了,下面看看效果:
播放前: \

 

播放时:

\

 

左边对应的listview:

\

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399082455&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3700985.html&dt=1399082457398&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399082457247&frm=20&ga_vid=429972749.1397695120&ga_sid=1399077957&ga_hid=1039274715&ga_fc=1&u_tz=480&u_his=321&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=2771&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=WlCUMfnc34&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399082455&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3700985.html&dt=1399082457447&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399082457247&frm=20&ga_vid=429972749.1397695120&ga_sid=1399077957&ga_hid=1039274715&ga_fc=1&u_tz=480&u_his=321&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=3021&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=eHXQmivVUd&p=http%3A//www.cnblogs.com&dtd=40" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>