android - 绘制行进路径的最佳方法

标签 android canvas gps trigonometry

我正在制作一个基于 GPS 坐标来跟踪车辆的应用程序。

我创建了一个SurfaceView,为他绘制田地、车辆和路径(路线)。

结果是这样的:

enter image description here

黑点代表GPS坐标的到来,蓝色矩形代表行进路径所覆盖的区域。 (路径的宽度是可配置的)

我用蓝色矩形绘制的方式(这是我的问题)是行进路径所覆盖的区域。 (路径的宽度是可配置的)

因此我需要克服一些情况。

  • 我需要计算 field 的旋转角度,以便路径始终落在后面。 (已完成)
  • 我需要计算每个矩形的旋转角度,使它们面向车辆。 (已完成)

以后我需要:

  • 检测车辆何时在同一个地方经过两次。 (基于行进的路径)
  • 计算车辆行驶的总面积(平方米)。

我想要一些绘制这条路径的提示。

我的代码:

public void draw(Canvas canvas) {
    Log.d(getClass().getSimpleName(), "draw");
    canvas.save();

    // translate canvas to vehicle positon
    canvas.translate((float) center.cartesian(0), (float) center.cartesian(1));

    float fieldRotation = 0;

    if (trackerHistory.size() > 1) {
         /*
        Before drawing the way, only takes the last position and finds the angle of rotation of the field.
         */
        Vector lastPosition = new Vector(convertToTerrainCoordinates(lastPoint));
        Vector preLastPosition = new Vector(convertToTerrainCoordinates(preLastPoint));
        float shift = (float) lastPosition.distanceTo(preLastPosition);

        /*
        Having the last coordinate as a triangle, 'preLastCoord' saves the values of the legs, while 'shift' is the hypotenuse
        */
        // If the Y offset is negative, then the opposite side is the Y displacement
        if (preLastPosition.cartesian(1) < 0) {
            // dividing the opposite side by hipetenusa, we have the sine of the angle that must be rotated.
            double sin = preLastPosition.cartesian(1) / shift;

            // when Y is negative, it is necessary to add or subtract 90 degrees depending on the value of X
            // The "Math.asin()" calculates the radian arc to the sine previously calculated.
            // And the "Math.toDegress()" converts degrees to radians from 0 to 360.
            if (preLastPosition.cartesian(0) < 0) {
                fieldRotation = (float) (Math.toDegrees(Math.asin(sin)) - 90d);
            } else {
                fieldRotation = (float) (Math.abs(Math.toDegrees(Math.asin(sin))) + 90d);
            }
        }
        // if not, the opposite side is the X offset
        else {
            // dividing the opposite side by hipetenusa have the sine of the angle that must be rotated.
            double senAngulo = preLastPosition.cartesian(0) / shift;

            // The "Math.asin()" calculates the radian arc to the sine previously calculated.
            // And the "Math.toDegress()" converts degrees to radians from 0 to 360.
            fieldRotation = (float) Math.toDegrees(Math.asin(senAngulo));
        }
    }

    final float dpiTrackerWidth = Navigator.meterToDpi(trackerWidth); // width of rect

    final Path positionHistory = new Path(); // to draw the route
    final Path circle = new Path(); // to draw the positions

    /*
    Iterate the historical positions and draw the path
    */
    for (int i = 1; i < trackerHistory.size(); i++) {
        Vector currentPosition = new Vector(convertToTerrainCoordinates(trackerHistory.get(i))); // vector with X and Y position
        Vector lastPosition = new Vector(convertToTerrainCoordinates(trackerHistory.get(i - 1))); // vector with X and Y position

        circle.addCircle((float) currentPosition.cartesian(0), (float) currentPosition.cartesian(1), 3, Path.Direction.CW);
        circle.addCircle((float) lastPosition.cartesian(0), (float) lastPosition.cartesian(1), 3, Path.Direction.CW);

        if (isInsideOfScreen(currentPosition.cartesian(0), currentPosition.cartesian(1)) ||
                isInsideOfScreen(lastPosition.cartesian(0), lastPosition.cartesian(1))) {
            /*
            Calcule degree by triangle sides
             */
            float shift = (float) currentPosition.distanceTo(lastPosition);
            Vector dif = lastPosition.minus(currentPosition);
            float sin = (float) (dif.cartesian(0) / shift);

            float degress = (float) Math.toDegrees(Math.asin(sin));

            /*
            Create a Rect to draw displacement between two coordinates
             */
            RectF rect = new RectF();
            rect.left = (float) (currentPosition.cartesian(0) - (dpiTrackerWidth / 2));
            rect.right = rect.left + dpiTrackerWidth;
            rect.top = (float) currentPosition.cartesian(1);
            rect.bottom = rect.top - shift;

            Path p = new Path();
            Matrix m = new Matrix();
            p.addRect(rect, Path.Direction.CCW);
            m.postRotate(-degress, (float) currentPosition.cartesian(0), (float) currentPosition.cartesian(1));
            p.transform(m);

            positionHistory.addPath(p);
        }
    }

    // rotates the map to make the route down.
    canvas.rotate(fieldRotation);

    canvas.drawPath(positionHistory, paint);
    canvas.drawPath(circle, paint2);

    canvas.restore();
}

我的目标是拥有类似这个应用程序的东西:https://play.google.com/store/apps/details?id=hu.zbertok.machineryguide (但目前只有 2D)

编辑:

进一步澄清我的疑虑:

  • 我对此没有太多经验。我想要一个更好的方法来绘制路径。对于矩形,它不是很好。请注意,曲线是一些空白区域。
  • 另一点是矩形的旋转,我在绘制时旋转它们。我相信这将使检测重叠变得困难
  • 我认为我需要数学帮助来旋转物体和重叠检测。它还有助于绘制填充形状的路径。

最佳答案

经过一段时间的研究,我得出了一个成功的结果。我将评论我的想法以及解决方案。

正如我在问题中解释的那样,沿途我有车辆行驶的坐标,还应该绘制路径宽度的设置。

使用 LibGDX库准备了许多功能,例如实现“正交相机”以处理定位、旋转等。

使用 LibGDX,我将我身边的 GPS 坐标转换为行驶的道路。像这样: enter image description here

下一个挑战是填补走过的路。首先我尝试使用矩形,但结果如我的问题所示。

因此解决方案是使用路径的边作为顶点来追踪三角形。像这样: enter image description here

然后简单地填充三角形。像这样: enter image description here

最后,我使用 Stencil 设置了 OpenGL 以突出显示重叠部分。像这样: enter image description here

已解决的其他问题:

  • 要计算覆盖面积,只需计算路径上现有三角形的面积即可。
  • 要检测重叠,只需检查车辆的当前位置是否在三角形内。

感谢:

  • AlexWien 的关注和他们的时间。
  • 康纳·安德森 videos of LibGDX
  • 特别感谢 Luis Eduardo 提供的知识,对我帮助很大。

sample source code .

关于android - 绘制行进路径的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056755/

相关文章:

java - 比较两个文本(一个来自按钮,点击后比较),Android

android - onperformsync 和多线程

javascript - 如何为 Canvas "drawing style"应用程序获取平滑的鼠标事件?

ubuntu - 如何使用 linux/ubuntu 从 canmore gporter gp-102+ 获取 gps 跟踪日志

android - 一次性确定用户位置(通过 GPS)的正确控制流程是什么?

android - 如何在android中的 ListView 中使每个项目的分隔符成为一部分?

安卓性能计

javascript - JavaScript 中的碰撞检测算法

html - Kineticjs 与 Raphaeljs

c++ - 在 Linux 中使用 gps timestamp_t 结构设置系统时间