Android 使用GPS实时记录运动路线

jopen 12年前

使用GPS实时记录运动路线,没有经过真机测试但是理论上应该没问题 等有流量后进行一下性能测试

主要思路就是使用list实时记录运动坐标 , 继承 Overlay,并重写draw方法,在draw方法中通过list的数值画出path


首先声明list

    private List<GeoPoint> gPointList = new ArrayList<GeoPoint>();  
继承Overlay
 class MyOverlay extends Overlay {            @Override            public void draw(Canvas canvas, MapView mapView, boolean shadow) {                 // TODO Auto-generated method stub                 super.draw(canvas, mapView, shadow);                              //Log.i(TAG, "draw");地图变化时候不断重绘              // 画笔                 Paint paint = new Paint();                 paint.setColor(Color.RED);                 paint.setDither(true);                 paint.setStyle(Paint.Style.STROKE);                 paint.setStrokeJoin(Paint.Join.ROUND);                 paint.setStrokeCap(Paint.Cap.ROUND);                 paint.setStrokeWidth(2);                 Projection projection = mapView.getProjection();                Path path = new Path();              if (gPointList.size()>1) {//2               Point p1 = new Point();                projection.toPixels(gPointList.get(0), p1);                 path.moveTo(p1.x, p1.y);                for (int i = 0; i < gPointList.size()-1; i++) {                    Point p2 = new Point();                     projection.toPixels(gPointList.get(i+1), p2);                     path.lineTo(p2.x, p2.y);                     Log.i(TAG, ""+p2.x+""+p2.y);                   }              canvas.drawPath(path, paint);// 画出路径        }                        }         }   

显示在map上

//绘制路径

   gpoint1 = new GeoPoint((int) (lat * 1000000),                      (int) (lng * 1000000));           gPointList.add(gpoint1);        myOverlay = new MyOverlay();        overlays.add(myOverlay);  

4月20日 发现 如果放到线程里 貌似更好 可以设置多少时间取样

    class ThreadGetRoute implements Runnable {                public void run() {                                        try {                        gpoint1 = new GeoPoint((int) (lat * 1000000),                                (int) (lng * 1000000));                        gPointList.add(gpoint1);                        myOverlay = new MyOverlay();                        overlays.add(myOverlay);                        Thread.sleep(10000);                            } catch (Exception e) {                        e.printStackTrace();                    }                        }            }  
Android 使用GPS实时记录运动路线