Dynamically draw lines between multiple GeoPoints in Android MapView

I am developing an application with several custom inscriptions on MapView - representing vessels. When choosing a vessel, I show my previous positions on the map, again with custom overlay elements, and I would like to associate them with the line.

Some of the relevant problems that I saw here were resolved by overriding the Draw method and hard-coding the GeoPoints coordinates in the Draw method. This does not help me at all, since I have a lot of glasses from different vessels, and I can’t write them hard to Draw.

Is there an easy way to just draw a line between GeoPoints inside a for loop used to display custom overlays?

Thank you in advance.

0
source share
1 answer

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; /*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); } } 
+2
source

All Articles