2017-04-24 71 views
0

如何在带有折线的谷歌地图中启用行车路线。任何一个有助于绘制两点之间的道路地图。谷歌地图中两点之间的多段线不在道路上(行车路线)

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

 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) { 

     } 

回答

0

我的代码被赋予你需要解析由谷歌API的方向JSON对象返回。它需要解码折线中的所有点才能完成导航。 您可以自己编写解析逻辑,也可以从一些很酷的现有项目(如https://github.com/akexorcist/Android-GoogleDirectionLibrary)获得帮助。

+0

添加第三方库在这方面不安全。所以我试图在android地图的本地功能中做到这一点。感谢您的意见。 –

相关问题