Dynamically draw lines between multiple GeoPoints in Android MapView

南笙酒味 提交于 2019-12-20 05:44:09

问题


I am developing an application that has a few custom overlays on a MapView - representing vessels. When selecting a vessel, I am showing its previous positions on the map, again with custom overlay items, and I would like to connect them with a line.

Some relevant problems I saw here were solved by overriding the Draw method, and by hard-coding the GeoPoints' coordinates in the Draw method. That does not help me at all, since I have many points from different vessels, and cannot hard-code them all into Draw.

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

Thanks you in advance.


回答1:


Use the Projection from the MapView in order to convert GeoPoints to "screen" points. After that you can use Path to draw the line that you want. The first point should 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 are done.

Below is a code from my draw() method that draws a polygon around a set of points. Note that you do not have to use path.close() as I did on 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);
    }
}


来源:https://stackoverflow.com/questions/7955411/dynamically-draw-lines-between-multiple-geopoints-in-android-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!