简体   繁体   English

在Android MapView中的多个GeoPoints之间动态绘制线条

[英]Dynamically draw lines between multiple GeoPoints in Android MapView

I am developing an application that has a few custom overlays on a MapView - representing vessels. 我正在开发一个在MapView上有一些自定义叠加层的应用程序 - 代表船只。 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. 我在这里看到的一些相关问题是通过重写Draw方法,并通过在Draw方法中对GeoPoints坐标进行硬编码来解决的。 That does not help me at all, since I have many points from different vessels, and cannot hard-code them all into Draw. 这对我没有任何帮助,因为我有很多来自不同血管的点,并且不能将它们全部硬编码到Draw中。

Is there a simple way to just draw a line between the GeoPoints inside the for loop used to display the custom overlays?? 有没有一种简单的方法可以在用于显示自定义叠加的for循环内的GeoPoints之间画一条线?

Thanks you in advance. 提前谢谢你。

Use the Projection from the MapView in order to convert GeoPoints to "screen" points. 使用MapView中的Projection将GeoPoints转换为“屏幕”点。 After that you can use Path to draw the line that you want. 之后,您可以使用Path绘制所需的行。 The first point should be specified with path.moveTo(x, y) and the rest with path.lineTo(x, y) . 第一个点应该用path.moveTo(x, y)指定,其余的用path.lineTo(x, y) At the end you call canvas.drawPath(path) and you are done. 最后你调用canvas.drawPath(path)就完成了。

Below is a code from my draw() method that draws a polygon around a set of points. 下面是我的draw()方法的代码,它围绕一组点绘制一个多边形。 Note that you do not have to use path.close() as I did on my code. 请注意,您不必像我在代码中那样使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM