问题描述
- 根据百度地图提供的一组经纬度画带有箭头的线,如何操作啊?
-
根据百度地图提供的一组经纬度画带有箭头的线,如何操作啊?现在想到的方法是继承Overlay重写draw
根据android提供的画布自定义画线和箭头,但是mBaiduMap.addOverlay();中间要填写的类型是OverlayOptions 可是继承的Overlay这么转换啊?mBaiduMap.addOverlay(new MyOverlay());?这个时我的自定义类
/**- 自定义Overlay
*/
class MyOverlay extends Overlay {
private Paint paint;
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// super.draw(canvas, mapView, shadow);
if (shadow == false) {
paint = new Paint();
paint.setStyle(Style.STROKE);// 设置非填充
paint.setStrokeWidth(8);// 笔宽5像素
// paint.setColor(Color.rgb(22, 22, 22));// 设置为红笔
paint.setColor(Color.RED);
paint.setAntiAlias(true);// 锯齿不显示int size = listInfo.size() - 1;
for (int i = 0; i < size; i++) {
// paint.setColor(Color.rgb(120 + i * 3, 120 + i * 3,
// 50 + i * 3));// 设置为红笔
// Point point = mMapView.getProjection().toPixels(
// listpoint.get(i).pt, null);
// Point point2 = mMapView.getProjection().toPixels(
// listpoint.get(i + 1).pt, null);Point point = mBaiduMap.getProjection().toScreenLocation(
listInfo.get(i));
Point point2 = mBaiduMap.getProjection().toScreenLocation(
listInfo.get(i + 1));JudgeDrawArrow(canvas, point.x, point.y, point2.x, point2.y);// 画箭头
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);// 画线
}
// canvas.drawLine(listpoint.size().x, point.y, point2.x,
// point2.y, paint);// 画线
// Point point3 = mMapView.getProjection().toPixels(
// listpoint.get(size - 1).pt, null);
// paint.setColor(Color.BLUE);// 设置为红笔
// paint.setStyle(Style.STROKE);// 设置非填充
// paint.setStrokeWidth(5);// 笔宽5像素
// canvas.drawCircle(point3.x, point3.y, 25.5f, paint);}
if (shadow == false) {
paint.setStrokeWidth(5);// 笔宽5像素
// Point point4 = mMapView.getProjection().toPixels(
// listpoint.get(listpoint.size() - 2).pt, null);
Point point4 = mBaiduMap.getProjection().toScreenLocation(
listInfo.get(listInfo.size() - 2));
canvas.drawText("--》倒数第二站", point4.x, point4.y, paint);
}
}/**
- 画箭头
- @param canvas
- @param x1
- @param y1
- @param x2
- @param y2
*/
public void JudgeDrawArrow(Canvas canvas, int x1, int y1, int x2, int y2) {
double arrow_height = 25; // 箭头高度
double arrow_btomline = 12; // 底边的一半
int x3 = 0;
int y3 = 0;
int x4 = 0;
int y4 = 0;
double arctangent = Math.atan(arrow_btomline / arrow_height); // 箭头角度
double arrow_len = Math.sqrt(arrow_btomline * arrow_btomline- arrow_height * arrow_height); // 箭头的长度
double[] endPoint_1 = rotateVec(x2 - x1, y2 - y1, arctangent, true,
arrow_len);
double[] endPoint_2 = rotateVec(x2 - x1, y2 - y1, -arctangent,
true, arrow_len);
double x_3 = x2 - endPoint_1[0]; // (x_3,y_3)是第一端点
double y_3 = y2 - endPoint_1[1];
double x_4 = x2 - endPoint_2[0]; // (x_4,y_4)是第二端点
double y_4 = y2 - endPoint_2[1];
Double X3 = new Double(x_3);
x3 = X3.intValue();
Double Y3 = new Double(y_3);
y3 = Y3.intValue();
Double X4 = new Double(x_4);
x4 = X4.intValue();
Double Y4 = new Double(y_4);
y4 = Y4.intValue();
// 画线
canvas.drawLine(x1, y1, x2, y2, paint);
// 画箭头的一半
canvas.drawLine(x2, y2, x3, y3, paint);
// 画箭头的另一半
canvas.drawLine(x2, y2, x4, y4, paint);
}
- arrow_height * arrow_height); // 箭头的长度
public double[] rotateVec(int px, int py, double ang, boolean isChlen,
double newLen) {double rotateResult[] = new double[2];
// 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
double vx = px * Math.cos(ang) - py * Math.sin(ang);
double vy = px * Math.sin(ang) + py * Math.cos(ang);
if (isChlen) {
double d = Math.sqrt(vx * vx + vy * vy);
vx = vx / d * newLen;
vy = vy / d * newLen;
rotateResult[0] = vx;
rotateResult[1] = vy;
}
return rotateResult;
}}
- 自定义Overlay
解决方案
先画一个线段,然后画箭头
解决方案二:
http://blog.csdn.net/column/details/android-jacksen-map.html