简体   繁体   English

如何在多个LatLong Android地图之间绘制路线?

[英]How to draw route between multiple LatLong Android map?

Am working on an Android project where I want to draw route between 2 point on Google Map. 我在一个Android项目上工作,我想在Google Map上的2点之间绘制路线。 I have successfully drawn route between source and destination point. 我已经成功绘制了源和目的地之间的路线。 But I have 1 problem in that, ie Some times I want to draw a path between more than 2 point, That time the code that I have written is drawing route between first and the last position and leaving the mid point position. 但是我有一个问题,即有时候我想在两个以上的点之间绘制一条路径,那时候我编写的代码正在绘制第一个和最后一个位置之间的路径,并离开中点位置。 What I exactly want is my route should go through the mid point to the destination point. 我真正想要的是我的路线应该经过中点到目的地。 How can I achieve this? 我该如何实现?

You can separately draw routes between three point. 您可以分别在三个点之间绘制路线。 So, first draw the route from starting point to mid point then from mid point to end point. 因此,首先绘制从起点到中点的路线,然后从中点到终点。 Then if you want add the required markers. 然后,如果要添加所需的标记。

You can use PolylineOptions from GoogleMaps API Dcoumented Here and keep adding all the points which should be a part of your route. 您可以使用此处的 GoogleMaps API中的PolylineOptions,并继续添加所有应包含在路线中的点。 You can do something like this 你可以做这样的事情

        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);
        }

Hope this solves your problem. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM