2017-04-24 21 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地圖的本地功能中做到這一點。感謝您的意見。 –

相關問題