【问题标题】:Drawing a direction on Google maps is not continuous在谷歌地图上绘制方向不是连续的
【发布时间】:2023-04-05 00:26:01
【问题描述】:

我正在使用这些设置来创建从一个位置到另一个位置的方向:

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(15);
polylineOptions.color(colorPrimary);
mMap.addPolyline(polylineOptions.addAll(points));

在地图上,它看起来像这样:

https://ibb.co/D13xc0T

如何在没有中断的情况下从一个点到另一个点画一条连续的线?

这是获取积分的代码:

if (direction != null) {
    List<Route> routes = direction.routes;
    Route route = routes.get(0);
    List<Leg> legs = route.legs;
    Leg leg = legs.get(0);
    List<Step> steps = leg.steps;
    for (Step step : steps) {
        String point = step.polyline.points;
        List<LatLng> points = PolyUtil.decode(point);
    }
}

【问题讨论】:

  • 显示您的points。你在折线上添加了什么?
  • @MaratZangiev pointsList&lt;LatLng&gt; 的列表。不正确吗?
  • 是的,没错。提供您的代码pointspoints 变量里面是什么?
  • @MaratZangiev 请查看我更新的问题。
  • 在您提供的代码中,您的points 变量是在for-loop 中创建的。您确定上面代码中的变量是 points 吗? (不是)

标签: android google-maps google-maps-api-3 android-maps google-directions-api


【解决方案1】:

您在地图上添加了许多PolylineOptions。它们没有相互连接,因此您会遇到这种中断。尝试将所有解码路径添加到一个公共数组中。

您编辑的代码:

ArrayList<LatLng> points = new ArrayList<LatLng>();
for (Step step : steps) {
        String point = step.polyline.points;
        points.addAll(PolyUtil.decode(point));
    }

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(15);
polylineOptions.color(colorPrimary);
mMap.addPolyline(polylineOptions.addAll(points));

【讨论】:

  • 感谢您的回答。让我试试你的解决方案,我会让你回来。
  • 我只是按照你的建议做了,但它只创建了我整行的一个部分,即最后一部分。你还有什么想法吗?
  • 将所有部件添加到points 数组。然后绘制折线。它应该被不间断地绘制。或者如果我理解你不对,请提供你的整个代码
【解决方案2】:

试试这个,它对我来说很好用。您也可以添加多个点。

mMap.addPolyline(new PolylineOptions().add(PointA).add(PointB).width(8f).color(Color.RED));

【讨论】:

  • 让我试着让你回来。
  • 感谢您的回答,但我得到了相同的行为。你还有什么想法吗?
  • 请给出你的观点。我想看看它被破坏的行为
  • 你在“点”上得到了正确的价值吗? ...请您打印所有值
  • 是的,我得到了正确的值。
【解决方案3】:

我遇到了折线断裂的同样问题。在这里找不到解决方案后,我偶然发现了一个已关闭的问题here。如果您只是添加调用 Googles API 的结果给出的所有步骤的所有点,您将不会得到一条连续的线,因为端点将被添加两次。一次作为步骤的结束,第二次作为下一步的开始。这就是为什么没有单条连续线以及为什么必须删除重复项的原因。在 Kotlin 中,调用 allPoints = allPoints.distinct() as ArrayList&lt;LatLng&gt; 非常简单。调用它会保持点的顺序,但会删除列表中的所有重复项。

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2017-11-18
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多