从地图上绘制的线获取所有坐标

时间:2012-07-09 15:36:29

标签: android google-maps openstreetmap

在我的MapView我能够点击地图创建任意数量的点,并且点通过线连接但我不想绘制任何可能与另一条线相交的线像这样的地图

enter image description here

无论如何都要获取线条的坐标以检查新线条是否与其他线条相交?

或者,如果其他人对如何检查这一点有任何建议,那也很好

这是我绘制线条的绘制方法

@Override
    public void draw(final Canvas canvas, final MapView mapView, final boolean shadow){
        super.draw(canvas, mapView, shadow);

        try{
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        paint.setAlpha(100);
        paint.setStyle(Style.STROKE);

        Path path = new Path();
        Point point1 = null;
        boolean runOnce = false;
        for(int i = 0;i<items.size();i++){
            Point point = new Point();
            mapView.getProjection().toPixels(items.get(i).mGeoPoint, point);

            if(!runOnce)
            {
                path.moveTo(point.x, point.y);
                point1 = new Point(point.x, point.y);
                runOnce = true;
            }
            else
            {
                path.lineTo(point.x, point.y);
            }
        }
        path.lineTo(point1.x, point1.y);
        canvas.drawPath(path, paint);

        path.lineTo(point1.x, point1.y);
        canvas.drawPath(path, paint);

        Paint paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setDither(true);
        paint2.setColor(Color.RED);
        paint2.setStrokeWidth(3);
        paint2.setAlpha(20);
        paint2.setStyle(Style.FILL);

        Path path2 = new Path();
        Point point2 = null;
        boolean runOnce2 = false;
        for(int i = 0; i < items.size(); i++)
        {
            Point point = new Point();
            mapView.getProjection().toPixels(items.get(i).mGeoPoint, point);

            if(!runOnce2)
            {
                path2.moveTo(point.x, point.y);
                point2 = new Point(point.x, point.y);
                runOnce2 = true;
            }
            else
            {
                path2.lineTo(point.x, point.y);
            }
        }

        path2.lineTo(point2.x, point2.y);
        canvas.drawPath(path2, paint2);
        }catch(Exception e){

        }
    }

1 个答案:

答案 0 :(得分:1)

你去: - )

paulbourke.net/geometry/lineline2d

告诉您如何以数学方式确定两个线段的交点。

如果您不希望您的终点计为交叉点,那么首先检查您的终点是否重叠。这可以使用if语句解决,如下所示:

if(Math.abs(x1 - x2) < EPSILON && Math.abs(y1 - y2) < EPSILON)

其中EPSILON是您的容错值,例如0.001。