2017-04-24 7 views
0

ポリラインを使用してGoogleマップで運転ルートを有効にする方法。いずれかの人が2つのポイント間で道路上に地図を描くのに役立つことができます。Googleマップの2つのポイント間に描かれたポリラインは道路(運転の方向)にありません

現在、マップ内の2つの点の間に単純な線が描かれています。私はそれを変更することはできません。

私のコードは、あなたがGoogleのディレクションAPIでJSONオブジェクトリターンを解析する必要が

 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

以下の通りです。完全なナビゲーションを行うには、ポリライン内のすべての点をデコードする必要があります。 あなた自身で構文解析ロジックを書くことも、https://github.com/akexorcist/Android-GoogleDirectionLibraryのようなクールな既存プロジェクトから助けを得ることもできます。

+0

サードパーティのライブラリを追加することは、この点では安全ではありません。だから私はアンドロイドマップのネイティブな機能でそれをやろうとしています。コメントしてくれてありがとう。 –

関連する問題