Use Projection
from MapView
to convert GeoPoints to on-screen points. After that, you can use Path
to draw the line you want. The first point must be specified with path.moveTo(x, y)
, and the rest with path.lineTo(x, y)
. At the end, you call canvas.drawPath(path)
, and you're done.
Below is the code from my draw () method, which draws a polygon around many points. Note that you do not need to use path.close()
, as in my code.
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow){ if(shadow){ if(isDrawing == false){ return; } Projection proj = mapView.getProjection(); boolean first = true; path.rewind(); 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(polygon.size() > 2){ path.close(); } canvas.drawPath(path, polygonPaint); super.draw(canvas, mapView, shadow); } }
Manos
source share