points = new ArrayList();

private final Projection projection;

private final Paint paint;

public RouteOverlay(Projection projection) {

this.projection = projection;

// 设置画笔

paint = new Paint();

paint.setColor(Color.RED);

// 画笔的粗细

paint.setStrokeWidth(3);

paint.setAntiAlias(true);

paint.setStrokeMiter(3);

paint.setStyle(Style.STROKE);

}

@Override

public void draw(Canvas canvas, MapView mapView, boolean shadow) {

super.draw(canvas, mapView, shadow);

if(shadow)

{

return;

}

// 这里使用系统提供的Path,Point来画图

Path drawingPath = new Path();

android.graphics.Point pixelPoint = new android.graphics.Point();

// 得到当前屏幕的高*宽

int width = canvas.getWidth();

int height = canvas.getHeight();

GeoPoint bottomRight = projection.fromPixels(width, height);

GeoPoint topLeft = projection.fromPixels(0, 0);

int maxLat = topLeft.getLatitudeE6();

int minLat = bottomRight.getLatitudeE6();

int minLon = topLeft.getLongitudeE6();

int maxLon = bottomRight.getLongitudeE6();

int pointsSize = points.size();

// 之前的布点是否在屏幕中

boolean preOutOfBounds = true;

// 之前的布点是否是新的一段路线的开始

boolean preWasMoveTo = true;

int moveToLat = Integer.MAX_VALUE;

int moveToLon = Integer.MIN_VALUE;

for(int i=0; i{

double pointLat = points.get(i).getLat();

int pointLatInt = (int)(pointLat * 1E6);

double pointLon = points.get(i).getLon();

int pointLonInt = (int)(pointLon * 1E6);

// 判断该点有没有超出屏幕显示的范围

boolean currentOutOfBounds = pointLatInt < minLat || pointLatInt > maxLat ||

pointLonInt < minLon || pointLonInt > maxLon;

// 起点或者先前节点和当前节点都在屏幕之外,将preWasMoveTo设为true‘

// 当前坐标在屏幕外,并且之前坐标也在屏幕外,则设置preWasMoveTo为true

if(i == 0 || (preOutOfBounds && currentOutOfBounds))

{

moveToLat = pointLatInt;

moveToLon = pointLonInt;

preWasMoveTo = true;

}

else

{

if(preWasMoveTo)

{

GeoPoint geoPoint = new GeoPoint(moveToLat, moveToLon);

// 将经纬度转换为屏幕画布像素上的点(称之为布点)

projection.toPixels(geoPoint, pixelPoint);

// 设置point.x, point.y为新一段路线的开始

drawingPath.moveTo(pixelPoint.x, pixelPoint.y);

preWasMoveTo = false;

}

GeoPoint geoPoint = new GeoPoint(pointLatInt, pointLonInt);

projection.toPixels(geoPoint, pixelPoint);

// 将最后的布点与该布点之间画一条线

drawingPath.lineTo(pixelPoint.x, pixelPoint.y);

}

preOutOfBounds = currentOutOfBounds;

}

canvas.drawPath(drawingPath, paint);

}

public void setPoints(ArrayListpoints) {

this.points = points;

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐