我正在制作一个应用程序来跟踪基于GPS坐标的矢量。
我创建了一个SurfaceView来为他绘制场地,车辆和路径(路线)。
结果如下:
黑点表示GPS坐标的到来,蓝色矩形表示行进路径所覆盖的区域。 (路径的宽度是可配置的)
我用蓝色矩形绘制的方式(这是我的问题),它是行进路径所覆盖的区域。 (路径的宽度是可配置的)
有了这个我需要克服一些情况。
将来我需要:
我想要一些提示画这条路。
我的代码:
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)
修改
为了澄清我的疑虑:
答案 0 :(得分:0)
通常使用图形库中的“path”方法绘制这样的路径。 在该库中,您可以创建折线,并给出线宽。 您还可以指定角落的填充方式。 (BEVEL_JOIN,MITTER_JOIN)
主要问题是在驾驶或后记时画出路径。 后记是没问题的。 在驾驶时画画可能有点棘手,以避免每秒重绘路径。
使用带有moveTo和lineTo的路径创建折线时,您可以设置线宽,图形库将为您完成所有操作。 然后就没有间隙,因为它是一条多边形线。
答案 1 :(得分:0)
经过一段时间的研究,我取得了圆满成功。我会评论我的想法以及解决方案是什么。
正如我所解释的那样,沿途我有车辆行进的坐标,并且还应绘制路径宽度的设置。
使用LibGDX库准备了许多功能,例如实现“正交相机”以处理定位,旋转等。
使用LibGDX,我将我身边的GPS坐标转换为行进的道路。像这样:
接下来的挑战是填补旅行的路径。首先我尝试使用矩形,但结果如我的问题所示。
因此解决方案是使用路径的一侧作为顶点来跟踪三角形。像这样:
然后只需填写三角形即可。像这样:
最后,使用Stencil,我设置了OpenGL以突出显示重叠。像这样:
修复了其他问题:
感谢: