【问题标题】:How to draw route between multiple LatLong Android map?如何在多个 LatLong Android 地图之间绘制路线?
【发布时间】:2018-07-01 08:26:45
【问题描述】:

我正在开发一个 Android 项目,我想在谷歌地图上的 2 点之间绘制路线。我已经成功地在源点和目标点之间绘制了路线。但是我有一个问题,即有时我想在超过 2 个点之间绘制一条路径,那时我编写的代码是在第一个位置和最后一个位置之间绘制路线并离开中点位置。我真正想要的是我的路线应该通过中点到达目的地。我怎样才能做到这一点?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您可以在三点之间分别绘制路线。因此,首先绘制从起点到中点的路线,然后从中点到终点。然后,如果您想添加所需的标记。

    【讨论】:

    • 使用您建议的方法,实际上需要冗余代码 sn-p 做同样的事情,并且在设备性能方面也将是昂贵的操作。如果有很多要点,那么您的方法就是一场噩梦。
    • 使用 Google API 中记录的 PolylineOptions,同样的任务会变得非常简单直接。请参考我的回答。
    【解决方案2】:

    您可以使用 GoogleMaps API Dcoumented Here 中的 PolylineOptions 并继续添加应该成为您路线一部分的所有点。你可以这样做

            ArrayList<LatLng> points;
            PolylineOptions lineOptions = null;
    
            // Traversing through all the routes
            for (int i = 0; i < result.size(); i++) {
                points = new ArrayList<>();
                lineOptions = new PolylineOptions();
    
                // Fetching i-th route
                List<HashMap<String, String>> path = result.get(i);
    
                // Fetching all the points in i-th route
                for (int j = 0; j < path.size(); j++) {
                    HashMap<String, String> point = path.get(j);
    
                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng position = new LatLng(lat, lng);
    
                    points.add(position);
                }
    
                // Adding all the points in the route to LineOptions
                lineOptions.addAll(points);
                lineOptions.width(10);
                lineOptions.color(Color.RED);
    
            }
    
            // Drawing polyline in the Google Map for the i-th route
            if(lineOptions != null) {
                mMap.addPolyline(lineOptions);
            }
    

    希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-15
      • 2012-01-03
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 2013-07-08
      相关资源
      最近更新 更多