高德地图自定义路线轨迹的重绘

来源:互联网 发布:淘宝多少好评一个心 编辑:程序博客网 时间:2024/06/05 18:51

1.高德地图重绘之场景还原

       最近,项目中开辟了新需求,要求虚拟路线进行地图重绘,今天就高德地图的重绘作个分享!

2.绘制方法的详解

①给定几个关键坐标点的经纬度

private LatLonPoint startPoint = new LatLonPoint(34.185642, 108.881905);private LatLonPoint endPoint = new LatLonPoint(34.214397, 108.963448);private LatLonPoint lat_one = new LatLonPoint(34.215748, 108.891869);private LatLonPoint lat_two = new LatLonPoint(34.233685, 108.900635);private LatLonPoint lat_three = new LatLonPoint(34.225311, 108.930992);private LatLonPoint lat_four = new LatLonPoint(34.224136, 108.955284);
②进行第一次轨迹绘制

private void drawPath() {   PolylineOptions po = new PolylineOptions();   for (int i = 0; i < list_latLatLonPoints.size(); i++) {      LatLng point = new LatLng(list_latLatLonPoints.get(i).getLatitude(), list_latLatLonPoints.get(i).getLongitude());      po.add(point);   }   po.width(30);   po.color(Color.BLUE);   po.zIndex(10);   mAMap.addPolyline(po);}
PolylineOptions是用来绘制轨迹的核心类;

③重绘轨迹

1.将轨迹点二分,拆分成更多的点

/** * 获取两点之间的中点坐标 */public void getCenterPoint(LatLonPoint a,LatLonPoint b){   //两点的纬度    double ax = a.getLatitude();   double bx = b.getLatitude();   //两点的经度   double ay = a.getLongitude();   double by = b.getLongitude();      //中点的经纬度   double cx = (ax + bx)/2;   double cy = (ay + by)/2;   //中点的坐标   LatLonPoint center_point = new LatLonPoint(cx, cy);   list_centerPoints.add(center_point);}
2.获取所有重绘坐标点

/** * 获取最终所有点重绘坐标 */public void getReDrawPoint(){   for(int i = 0;i<list_latLatLonPoints.size();i++){      if(i < list_latLatLonPoints.size()-1) {         final_latLatPoints.add(list_latLatLonPoints.get(i));         final_latLatPoints.add(list_centerPoints.get(i));      }else{         final_latLatPoints.add(list_latLatLonPoints.get(i));      }   }}
3.设置起始点图标

private void setMarker() {   LatLng startPoint = new LatLng(list_latLatLonPoints.get(0).getLatitude(),         list_latLatLonPoints.get(0).getLongitude());   LatLng endPoint = new LatLng(list_latLatLonPoints.get(list_latLatLonPoints.size() - 1).getLatitude(),         list_latLatLonPoints.get(list_latLatLonPoints.size() - 1).getLongitude());   addMarker(startPoint, R.drawable.ic_marker_start);   addMarker(endPoint, R.drawable.ic_marker_end);   addBgMarker(endPoint,R.drawable.ic_marker_bg);   mAMap.animateCamera(CameraUpdateFactory         .newCameraPosition(new CameraPosition(new LatLng(list_latLatLonPoints.get(list_latLatLonPoints.size() - 1).getLatitude(),               list_latLatLonPoints.get(list_latLatLonPoints.size() - 1).getLongitude()), 15,               0, 0)));}
4.进行真正意义的重绘

Handler handler = new Handler() {   public void handleMessage(Message msg) {      if (msg.what == 1) {         if(i<final_latLatPoints.size()){            LatLng point = new LatLng(final_latLatPoints.get(i).getLatitude(), final_latLatPoints.get(i).getLongitude());            pol.add(point);            pol.width(30-(len+1)*3);            //pol.color(Color.GREEN);            //pol.color(Color.GREEN);            pol.color(colist.get(len));            pol.zIndex(10);            mAMap.addPolyline(pol);            i++;         }else{            if(len < colist.size()-1){               i = 0;               len++;               pol = null;               pol = new PolylineOptions();            }/*else if(len == colist.size()-1){               Toast.makeText(MainActivity.this,"已经跑了好久了,休息一下吧!",Toast.LENGTH_SHORT).show();            }*/         }      }      super.handleMessage(msg);   };};

3.Time定时器的详解

这里的重绘,我是通过定时器Timer实现的:

private void startTimer(){   if (mTimer == null) {      mTimer = new Timer();   }   if (mTimerTask == null) {      mTimerTask = new TimerTask() {         @Override         public void run() {            Message message = new Message();            message.what = 1;            handler.sendMessage(message);            do {               try {                  Log.i(TAG, "sleep(1000)...");                  Thread.sleep(1000);               } catch (InterruptedException e) {               }            } while (isPause);         }      };   }   if(mTimer != null && mTimerTask != null )      mTimer.schedule(mTimerTask, delay, period);}
这里mTimer.schedule(mTimerTask, delay , period) 第一个参数是task,第二个参数是延迟多长时间再触发定时器,第三个参数是持续的时间;注意:第三个参数一定得带上,不然定时器起不了定时的作用,这感觉是废话,哈哈!

效果图:


好了,今天到此为止!在实战中学习,在实战中精进;我是张星,欢迎您的关注!

2 0
原创粉丝点击