我有这个代码用于地图点之间的绘制线
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
if (!shadow) {
Projection projection = mapview.getProjection();
for(int i=0; i< puntos.size()-1; i++) {
Point origen = new Point();
Point destino = new Point();
projection.toPixels(puntos.get(i).getPoint(), origen);
projection.toPixels(puntos.get(i+1).getPoint(), destino);
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint);
mapview.invalidate();
}
}
super.draw(canvas, mapview, shadow);
}
但是当我将地图中的点太近或者指向该线与其他线相交时,地图视图会将多条线绘制到近点。所以,如果我有点1,2,3,4。点1有一条画线指向2,3,4
任何想法如何解决这个问题?
答案 0 :(得分:0)
尝试以下代码。它还将填充一些颜色的形状。您可以省略该功能。
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setStyle(Style.FILL_AND_STROKE);
mPaint.setColor(Color.RED);
mPaint.setAlpha(9);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
Path path = new Path();
Projection projection = mapView.getProjection();
for(int j = 0; j < geoArrayist.size(); j++)
{
Iterator<GeoPoint> it = geoArrayist.iterator();
while(it.hasNext())
{
GeoPoint arrayListGeoPoint = it.next();
Point currentScreenPoint = new Point();
projection.toPixels(arrayListGeoPoint, currentScreenPoint);
if(j == 0)
path.moveTo(currentScreenPoint.x, currentScreenPoint.y);
else
path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
}
}
canvas.drawPath(path, mPaint);
答案 1 :(得分:0)
更改您的代码:
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
if (!shadow) {
if(puntos.size() == 0) return;
//initialization
Point origen = new Point();
Point destino = new Point();
Paint paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
//end of initialization
Projection projection = mapview.getProjection();
projection.toPixels(puntos.get(0).getPoint(), origen);
for(int i=1; i< puntos.size(); i++) {
projection.toPixels(puntos.get(i).getPoint(), destino);
canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint);
}
}
super.draw(canvas, mapview, shadow);
}
理想情况下,您应该将上面的初始化块中的代码移动到overlay构造函数,并创建对象origen
,destino
和paint
全局变量。这样做,它将提高内存利用率,仅在覆盖的生命周期内创建每个对象1个。