【问题标题】:How to draw path between multiple markers in android如何在android中的多个标记之间绘制路径
【发布时间】:2016-01-10 18:43:37
【问题描述】:

您好,我正在使用 android。我创建了一个应用程序来定位用户的位置。我可以绘制带有相应纬度和经度数组列表值的标记。现在我想在这些标记之间绘制路径。我使用下面的代码来绘制两个标记。那么如何按数组列表的顺序连接所有标记。请帮助我。提前致谢。

private class ParserTask extends
            AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

        @Override
        protected List<List<HashMap<String, String>>> doInBackground(
                String... jsonData) {

            JSONObject jObject;
            List<List<HashMap<String, String>>> routes = null;

            try {
                jObject = new JSONObject(jsonData[0]);
                PathJsonParser parser = new PathJsonParser();
                routes = parser.parse(jObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return routes;
        }

        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
            ArrayList<LatLng> points = null;
            PolylineOptions polyLineOptions = null;

            try {

                // traversing through routes
                for (int i = 0; i < routes.size(); i++) {
                    points = new ArrayList<LatLng>();
                    polyLineOptions = new PolylineOptions();
                    List<HashMap<String, String>> path = routes.get(i);

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

                    polyLineOptions.addAll(points);
                    polyLineOptions.width(2);
                    polyLineOptions.color(Color.BLUE);
                }

                googleMap.addPolyline(polyLineOptions);
            }catch(NullPointerException e) {

                Toast.makeText(
                        getActivity(),
                        "Please check your internet connection !",
                        Toast.LENGTH_LONG).show();
                //If data == null it will pass here
            }

        }
    }


    private String getMapsApiDirectionsUrl() {
        String waypoints = "waypoints=optimize:true|"
                + start.latitude + "," + start.latitude;

        String sensor = "sensor=false";
        String params = waypoints + "&" + sensor;
        String output = "json";
        String url = "https://maps.googleapis.com/maps/api/directions/"
                + output + "?" + params;
        return url;
    }

【问题讨论】:

标签: android dictionary marker


【解决方案1】:

使用这个

Polyline line = map.addPolyline(new PolylineOptions().width(3).color(Color.RED));
line.setPoints(points);

其中点是 LatLng 列表

【讨论】:

  • 请注意:使用折线在标记之间画一条线,忽略道路(越野),因此该线可能会穿过建筑物,例如,这就是为什么最好从谷歌获取相邻 LatLong 的列表Directions api,它给出了实际在路上的点的准确列表,然后使用折线。参考这个链接:stackoverflow.com/questions/33094240/…
  • @NouranS.Ahmad 给定的链接未显示如何在多个标记之间绘制路径。是否有任何有效的链接
【解决方案2】:

试试这个就行了

PolylineOptions lineOptions = new PolylineOptions();

lineOptions.setPoints(points);
polyline = mMap.addPolyline(lineOptions);
        polyline.setWidth(5);
        polyline.setColor(Color.BLACK);

其中pointsLatlng 类型的ArrayList

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 2019-02-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多