百度地图 Android API 由自定义点绘制路线

参考了这篇文章:http://menuz.iteye.com/blog/1300662

View Code
  1 import java.util.List;
  2 
  3 import android.graphics.Canvas;
  4 import android.graphics.Color;
  5 import android.graphics.Paint;
  6 import android.graphics.Paint.Style;
  7 import android.graphics.Path;
  8 import android.graphics.Point;
  9 
 10 import com.android.lovepick.util.PathInfo;
 11 import com.baidu.mapapi.GeoPoint;
 12 import com.baidu.mapapi.MapView;
 13 import com.baidu.mapapi.Overlay;
 14 import com.baidu.mapapi.Projection;
 15 
 16 public class PathOverlay extends Overlay
 17 {
 18     // private final String PATH_OVERLAY_TAG = "PathOverlay";
 19     private List<PathInfo> mPathInfos = null;
 20     private Projection mProjection;
 21     private Paint mPaint = null;
 22 
 23     public PathOverlay(Projection projection, List<PathInfo> pathInfos)
 24     {
 25         this.mProjection = projection;
 26         this.mPathInfos = pathInfos;
 27 
 28         // setting the paint
 29         mPaint = new Paint();
 30         mPaint.setColor(Color.BLUE);
 31         mPaint.setStrokeWidth(10);
 32         mPaint.setAntiAlias(false);
 33         mPaint.setStrokeMiter(3);
 34         mPaint.setStyle(Style.STROKE);
 35     }
 36 
 37     @Override
 38     public void draw(Canvas canvas, MapView mapView, boolean shadow)
 39     {
 40         super.draw(canvas, mapView, shadow);
 41         if (shadow)
 42         {
 43             return;
 44         }
 45 
 46         Path path = new Path();
 47         Point pixelPoint = new Point();
 48 
 49         // 获取屏幕高度和宽度
 50         int width = canvas.getWidth();
 51         int height = canvas.getHeight();
 52 
 53         GeoPoint bottomRight = mProjection.fromPixels(width, height);
 54         GeoPoint topLeft = mProjection.fromPixels(0, 0);
 55 
 56         int maxLat = topLeft.getLatitudeE6();
 57         int minLat = bottomRight.getLatitudeE6();
 58         int minLng = topLeft.getLongitudeE6();
 59         int maxLng = bottomRight.getLongitudeE6();
 60 
 61         // 之前的布点是否在屏幕中
 62         boolean preOutOfBounds = true;
 63         // 之前的布点是否是新的一段路线的开始
 64         boolean preIsMoveTo = true;
 65 
 66         int moveToLat = Integer.MAX_VALUE;
 67         int moveToLng = Integer.MIN_VALUE;
 68 
 69         boolean first = true;
 70 
 71         for (PathInfo pathInfo : mPathInfos)
 72         {
 73             // Log.i(PATH_OVERLAY_TAG, "pathInfo: " + pathInfo.toString());
 74             int latE6 = (int) (Double.parseDouble(pathInfo.getLatitude()) * 1E6);
 75             int lngE6 = (int) (Double.parseDouble(pathInfo.getLongitude()) * 1E6);
 76 
 77             // 判断当前点是否超出屏幕显示的范围
 78             boolean currentOutOfBounds = (latE6 < minLat) || (latE6 > maxLat)
 79                     || (lngE6 < minLng) || (lngE6 > maxLng);
 80 
 81             // 起点, preIsMoveTo = true;
 82             // 先前点和当前点都在屏幕之外, preIsMoveTo = true;
 83             // 当前坐标在屏幕外, 并且先前坐标也在屏幕外, preIsMoveTo = true.
 84             if (first || (preOutOfBounds && currentOutOfBounds))
 85             {
 86                 moveToLat = latE6;
 87                 moveToLng = lngE6;
 88                 path.moveTo(pixelPoint.x, pixelPoint.y);
 89                 first = false;
 90                 preIsMoveTo = true;
 91             }
 92             else
 93             {
 94                 if (preIsMoveTo)
 95                 {
 96                     // 绘制新的一段路线的起点
 97                     GeoPoint geoPoint = new GeoPoint(moveToLat, moveToLng);
 98                     // 将经纬度转换成屏幕画布上的点(即布点)
 99                     mProjection.toPixels(geoPoint, pixelPoint);
100 
101                     // 绘制新路线的起点
102                     path.moveTo(pixelPoint.x, pixelPoint.y);
103                     preIsMoveTo = false;
104                 }
105                 GeoPoint geoPoint = new GeoPoint(latE6, lngE6);
106                 mProjection.toPixels(geoPoint, pixelPoint);
107                 // 最后的布点与当前布点之间画一条线.
108                 path.lineTo(pixelPoint.x, pixelPoint.y);
109             }
110             preOutOfBounds = currentOutOfBounds;
111         }
112         canvas.drawPath(path, mPaint);
113     }
114 
115 }

代码中的PathInfo是自己定义的一个类,存储了点的经纬度(用字符串存的)

View Code
 1 public class PathInfo
 2 {
 3     private String latitude;
 4     private String longitude;
 5 
 6     public String getLatitude()
 7     {
 8         return latitude;
 9     }
10 
11     public void setLatitude(String latitude)
12     {
13         this.latitude = latitude;
14     }
15 
16     public String getLongitude()
17     {
18         return longitude;
19     }
20 
21     public void setLongitude(String longitude)
22     {
23         this.longitude = longitude;
24     }
25 
26     @Override
27     public String toString()
28     {
29         return "{lat:" + latitude + "," + "lng:" + longitude + "}";
30     }
31 }

根据这篇文章的作者的写法,优化效果还不错,地图移动挺流畅的。

附上一张效果图:

posted @ 2012-10-18 18:32  longfloat  阅读(1031)  评论(0)    收藏  举报