2013-03-14 15 views
8

地図上に描画パスを描画するためにdrawPath()が使用されます。アンドロイドでウェイポイントを使用してルートを描く描画ルート(Googleマップ、Google方向API、json解析、Googleポリラインのデコード)

public void drawPath(String result){ 
     try { 
      final JSONObject jsonObject = new JSONObject(result); 

      JSONArray routeArray = jsonObject.getJSONArray("routes"); 
      JSONObject routes = routeArray.getJSONObject(0); 


      JSONObject overviewPolylines = routes.getJSONObject("overview_polyline"); 
      String encodedString = overviewPolylines.getString("points"); 

      String statusString = jsonObject.getString("status"); 

      Log.d("test: ", encodedString); 
      List<LatLng> list = decodePoly(encodedString); 

      LatLng last = null; 
      for (int i = 0; i < list.size()-1; i++) { 
       LatLng src = list.get(i); 
       LatLng dest = list.get(i+1); 
       last = dest; 
       Log.d("Last latLng:", last.latitude + ", " + last.longitude); 
       Polyline line = mMap.addPolyline(new PolylineOptions() 
       .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)) 
       .width(4) 
       .color(Color.GREEN)); 
      } 

      Log.d("Last latLng:", last.latitude + ", " + last.longitude); 
     }catch (JSONException e){ 
      e.printStackTrace(); 
     } 
     catch(ArrayIndexOutOfBoundsException e) { 
      System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage()); 
     } 
    } 

    private List<LatLng> decodePoly(String encoded){ 


     List<LatLng> poly = new ArrayList<LatLng>(); 
     int index = 0; 
     int length = encoded.length(); 

     int latitude = 0; 
     int longitude = 0; 

     while(index < length){ 
      int b; 
      int shift = 0; 
      int result = 0; 

      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 

      int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      latitude += destLat; 

      shift = 0; 
      result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b > 0x20); 

      int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      longitude += destLong; 

      poly.add(new LatLng((latitude/1E5),(longitude/1E5))); 
     } 
     return poly; 
    } 

私の問題は、リンクにウェイポイントがあるため、ルートが地図に正しく表示されないことです。私は私のリンクは、私はあなたのdecodePolyの一部をテストしている http://maps.googleapis.com/maps/api/directions/json?origin=18.XXX,73.XXXdestination=18.XXX,73.XXX&sensor=false&units=metric&mode=driving&waypoints=via:18.XXX,73.XXX

答えて

2

であるアンドロイド4.2

でそれを使用しています。そこに問題はないようだ。より正確なステップとレッグを使用することができます。

1

@ Zeratul overview_polyline.points以上のコードをデコードしていただきありがとうございます。それは素晴らしい作品です。ちなみに、LatLngのリストを取得した後、リストをループする必要はありません。 PolylineOptionsのaddAllメソッドを使用してください。

List<LatLng> list = decodePoly(encodedString); 
PolylineOptions po = new PolylineOptions(); 
po.addAll(list); 
po.width(2).color(Color.BLUE); 
Polyline line = getMap().addPolyline(po); 
関連する問題