谷歌地图中两点之间绘制的折线不在道路上(行车路线)

时间:2017-04-24 12:10:29

标签: android google-maps android-studio polyline

如何使用折线在Google地图中启用行车路线。任何人都可以帮助在两点之间绘制地图。

目前在地图中的两个点之间绘制一条简单的线。我无法改变它。

我的代码如下:

        String stringUrl = "http://maps.googleapis.com/maps/api/directions/json?origin=" + fromPosition+ "&destination=" + toPosition+ "&sensor=false";
        StringBuilder response = new StringBuilder();
        try {
            URL url = new URL(stringUrl);
            HttpURLConnection httpconn = (HttpURLConnection) url
                    .openConnection();
            if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader input = new BufferedReader(
                        new InputStreamReader(httpconn.getInputStream()),
                        8192);
                String strLine = null;

                while ((strLine = input.readLine()) != null) {
                    response.append(strLine);
                }
                input.close();
            }

            String jsonOutput = response.toString();

            JSONObject jsonObject = new JSONObject(jsonOutput);

            // routesArray contains ALL routes
            JSONArray routesArray = jsonObject.getJSONArray("routes");
            // Grab the first route
            JSONObject route = routesArray.getJSONObject(0);

            JSONArray legs = route.getJSONArray("legs");
            JSONObject firtsLegs = legs.getJSONObject(0);

            JSONObject distance = firtsLegs.getJSONObject("distance");


            System.out.println("Response test was : " + distance.getString("text"));

            String walkDistance = distance.getString("text");



            JSONObject poly = route.getJSONObject("overview_polyline");
            String polyline = poly.getString("points");
            decodePoly(polyline);


        } catch (Exception e) {

        }

1 个答案:

答案 0 :(得分:0)

您需要通过Google Direction API解析JSON对象返回。它需要解码折线中的所有点以进行完整导航。 您既可以自己编写解析逻辑,也可以从一些很酷的现有项目中获取帮助,例如https://github.com/akexorcist/Android-GoogleDirectionLibrary

相关问题