简体   繁体   English

在点之间绘制路径

[英]Drawing a path between points

I am working in Google Maps. 我正在使用Google地图。 I want to create a Path between 5 to 6 nodes, I mean just drawing a line between points. 我想在5到6个节点之间创建路径,我的意思是只在点之间画一条线。

The following class is an inner class for drawing: 以下类是用于绘制的内部类:

class MyOverlay extends Overlay{
    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);

        Paint  mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        GeoPoint gP1 = new GeoPoint(19240000,-99120000);
        GeoPoint gP2 = new GeoPoint(37423157, -122085008);
        GeoPoint gP3 = new GeoPoint(20000000 , -50000000) ; 
        GeoPoint[] G = new GeoPoint[3] ;
        G[1] = gP1 ; 
        G[2] = gP2 ; 
        G[3] = gP3 ; 
        Point p1 = new Point();
        Point p2 = new Point();
        Point p3 = new Point () ; 
        Path path = new Path();

        projection.toPixels(gP1, p1);
        projection.toPixels(gP2, p2);
        projection.toPixels(gP3, p3) ; 
        //path.moveTo(p2.x, p2.y);
        //path.lineTo(p1.x,p1.y);
        //path.lineTo(p3.x, p3.y) ;       
        //canvas.drawLines(pts , mPaint) ; 
        canvas.drawPath(path, mPaint);
    }
}

These two lines of code are the code from the onCreate() method in the outer class: 这两行代码是外部类中onCreate()方法的代码:

 projection = mapView.getProjection();

 mapOverlays.add(new MyOverlay());

The above answers are correct - the problem is you are not adding a Point to your Path . 以上答案是正确的-问题是您没有在Path添加Point See this answer for something exactly similar to what you're doing. 请参阅答案以获取与您正在执行的操作完全相似的操作。 Also, draw() is executed EVERY time the map needs to redraw the route, which is basically anytime the map moves.I have an app where I draw a route on the MapView and let me tell you, if your route gets long your map can get laggy. 此外, draw()每次执行地图需要重新绘制的路线,这基本上是随时在地图moves.I有一个应用程序,我借鉴的路线MapView ,让我告诉你,如果你就可以得到长期地图会变得迟钝。 So I would put your Paint stuff inside your class's constructor, so you're not creating a new object every time the route is redrawn. 因此,我会将您的Paint东西放入类的构造函数中,这样您就不会在每次重画路径时都创建一个新对象。 Also, if you're planning to draw multiple points, I would use a for loop similar to this: 另外,如果您打算绘制多个点,则可以使用类似于以下的for循环:

Path path = new Path();
for(GeoPoint g : arrayOfGeoPoints) {
            Point next = new Point();
            projection.toPixels(g, next);
            path.lineTo(next.x, next.y);
            path.moveTo(next.x, next.y);
        }
canvas.drawPath(path, myPaint);

It's simple, but can iterate through many points quickly and efficiently. 它很简单,但是可以快速高效地遍历许多点。

Good luck! 祝好运!

I'm not familiar with GeoPoint or MapView, so you may have other problems, but I can point out the obvious: 我对GeoPoint或MapView不熟悉,因此您可能还有其他问题,但是我可以指出一个明显的问题:

You've commented out the lines where you generate the Path, so you're drawing an empty Path. 您已注释掉生成路径的行,因此绘制了一个空的路径。 If you uncomment the three lines starting with path. 如果您取消注释以path.开头的三行path. (but leave canvas.drawLines(...) commented), you should have a line from p2 to p1 and another one from p1 to p3 , assuming there isn't anything else wrong. (但请保留canvas.drawLines(...)注释),假设没有其他错误,您应该p2p1有一条线,而在p1p3有另一条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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