【问题标题】:Polyline is not getting drawn on the google map折线未在谷歌地图上绘制
【发布时间】:2016-11-27 06:13:12
【问题描述】:

我的数组列表中有一组 LatLngs,我想使用这些 LatLngs 在地图上绘制一条折线。我将我的 latlngs 添加到 PolylineOptions,然后绘制折线。但是折线没有被绘制出来。

这是我的代码 sn-p。

PolylineOptions options = new  
PolylineOptions().width(5).color(Color.RED).geodesic(true);

for (int k = 0; k < latLngs.size()-1; k++) {
LatLng point = latLngs.get(i);                 
options.add(point);
}
mMap.addPolyline(options);

【问题讨论】:

  • 你可以使用 options.addAll(latLngs); - 您可能还想验证 latLngs 是否为空。
  • 还有小事,您的循环将提前结束一项。您正在检查小于长度减 1。因此,如果您有一个长度为 5 的数组,那么当您的循环到达第 5 项索引 4 时,它将在运行内部条件语句之前结束。如果您检查小于或等于,则只需从长度中减去 1。在这种情况下,您可以只检查索引是否小于长度。
  • 我的 latlngs 数组列表不为空 Angel Koh
  • 是的,我已经改变了@idratherbeintheair
  • 添加点的循环使用 k 作为索引变量,而您使用 i 从 ArrayList 中提取项目的任何事实?

标签: android google-maps google-polyline


【解决方案1】:

试试这个

PolylineOptions options = new PolylineOptions().width(10).color(ContextCompat.getColor(context, R.color.color_primary)); 
options.add(new LatLng(23.229163, 72.658540)); 
options.add(new LatLng(23.212809, 72.648331)); 
options.add(new LatLng(23.214441, 72.644304));

Polyline polyLine = mMap.addPolyline(options);  
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.229163, 72.658540), 17.0f));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 2018-03-06
    • 2013-07-02
    • 2023-04-06
    • 2014-03-19
    相关资源
    最近更新 更多