使用GPS实时记录运动路线,没有经过真机测试但是理论上应该没问题 等有流量后进行一下性能测试
继承Overlay
主要思路就是使用list实时记录运动坐标 , 继承 Overlay,并重写draw方法,在draw方法中通过list的数值画出path。
首先声明list
1 | private List<GeoPoint> gPointList = new ArrayList<GeoPoint>(); |
01 | class MyOverlay extends Overlay { |
02 | @Override |
03 | public void draw(Canvas canvas, MapView mapView, boolean shadow) { |
04 | // TODO Auto-generated method stub |
05 | super.draw(canvas, mapView, shadow); |
06 | |
07 | //Log.i(TAG, "draw");地图变化时候不断重绘 |
08 | // 画笔 |
09 | Paint paint = new Paint(); |
10 | paint.setColor(Color.RED); |
11 | paint.setDither(true); |
12 | paint.setStyle(Paint.Style.STROKE); |
13 | paint.setStrokeJoin(Paint.Join.ROUND); |
14 | paint.setStrokeCap(Paint.Cap.ROUND); |
15 | paint.setStrokeWidth(2); |
16 | Projection projection = mapView.getProjection(); |
17 | Path path = new Path(); |
18 | if (gPointList.size()>1) {//2 |
19 | Point p1 = new Point(); |
20 | projection.toPixels(gPointList.get(0), p1); |
21 | path.moveTo(p1.x, p1.y); |
22 | for (int i = 0; i < gPointList.size()-1; i++) { |
23 | Point p2 = new Point(); |
24 | projection.toPixels(gPointList.get(i+1), p2); |
25 | path.lineTo(p2.x, p2.y); |
26 | Log.i(TAG, ""+p2.x+""+p2.y); |
27 | |
28 | } |
29 | canvas.drawPath(path, paint);// 画出路径 |
30 | } |
31 | |
32 | } |
33 | } |
显示在map上
//绘制路径
1 | gpoint1 = new GeoPoint((int) (lat * 1000000), |
2 | (int) (lng * 1000000)); |
3 | gPointList.add(gpoint1); |
4 | myOverlay = new MyOverlay(); |
5 | overlays.add(myOverlay); |
本文介绍了一种使用GPS实时记录运动坐标的方法,通过列表存储坐标数据并在地图上绘制路径。主要思路是在地图视图中继承Overlay类,重写draw方法,通过遍历坐标列表并使用画笔在地图上绘制路径。实现步骤包括声明坐标列表、继承Overlay类、绘制地图上的路径。通过这种方式,可以实时展示运动轨迹。
3415

被折叠的 条评论
为什么被折叠?



