[英]Not able to draw path between two geopoints in android mapview?
[英]Dynamically draw lines between multiple GeoPoints in Android MapView
我正在開發一個在MapView上有一些自定義疊加層的應用程序 - 代表船只。 選擇船只時,我在地圖上顯示其先前的位置,再次使用自定義疊加項目,我想用一條線連接它們。
我在這里看到的一些相關問題是通過重寫Draw方法,並通過在Draw方法中對GeoPoints坐標進行硬編碼來解決的。 這對我沒有任何幫助,因為我有很多來自不同血管的點,並且不能將它們全部硬編碼到Draw中。
有沒有一種簡單的方法可以在用於顯示自定義疊加的for循環內的GeoPoints之間畫一條線?
提前謝謝你。
使用MapView
中的Projection
將GeoPoints轉換為“屏幕”點。 之后,您可以使用Path
繪制所需的行。 第一個點應該用path.moveTo(x, y)
指定,其余的用path.lineTo(x, y)
。 最后你調用canvas.drawPath(path)
就完成了。
下面是我的draw()方法的代碼,它圍繞一組點繪制一個多邊形。 請注意,您不必像我在代碼中那樣使用path.close()
。
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow){
if(shadow){
if(isDrawing == false){
return;
}
Projection proj = mapView.getProjection();
boolean first = true;
/*Clear the old path at first*/
path.rewind();
/* The first tap */
Paint circlePaint = new Paint();
Point tempPoint = new Point();
for(GeoPoint point: polygon){
proj.toPixels(point, tempPoint);
if(first){
path.moveTo(tempPoint.x, tempPoint.y);
first = false;
circlePaint.setARGB(100, 255, 117, 0);
circlePaint.setAntiAlias(true);
canvas.drawCircle(tempPoint.x, tempPoint.y, FIRST_CIRCLE_RADIOUS, circlePaint);
}
else{
path.lineTo(tempPoint.x, tempPoint.y);
circlePaint.setARGB(100, 235, 0, 235);
circlePaint.setAntiAlias(true);
canvas.drawCircle(tempPoint.x, tempPoint.y, CIRCLE_RADIOUS, circlePaint);
}
}
/* If indeed is a polygon just close the perimeter */
if(polygon.size() > 2){
path.close();
}
canvas.drawPath(path, polygonPaint);
super.draw(canvas, mapView, shadow);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.