2016-04-25 21 views
2

私のアプリは、ユーザーの位置とポイントとの間のルートを描画するオプションを既に実装しています。googlemapsApiで2つ以上のルートを描画する方法

は、今私は5箇所にコードを適応し、それらの間でマップ上に描画する必要が

マイコード:

public void getRoute(final LatLng origin, final LatLng destination) { 

    new Thread() { 
     public void run() { 

      int l = 0; 
        l++; 

      String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" 
        + origin.latitude + "," + origin.longitude + "&destination=" 
        + destination.latitude + "," + destination.longitude + "&sensor=true&mode=walking&alternatives=true&region=pt"; 

      HttpResponse response; 
      HttpGet request; 
      AndroidHttpClient client = AndroidHttpClient.newInstance("route"); 

      request = new HttpGet(url); 
      try { 
       response = client.execute(request); 
       final String answer = EntityUtils.toString(response.getEntity()); 

       runOnUiThread(new Runnable() { 
        public void run() { 
         try { 

          list = buildJSONRoute(answer); 
          drawRoute(); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }.start(); 
} 


public List<LatLng> buildJSONRoute(String json) throws JSONException { 
    JSONObject result = new JSONObject(json); 
    JSONArray routes = result.getJSONArray("routes"); 




    JSONArray steps = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps"); 

    List<LatLng> lines = new ArrayList<LatLng>(); 

    for (int i = 0; i < steps.length(); i++) { 


     String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points"); 

     for (LatLng p : decodePolyline(polyline)) { 
      lines.add(p); 
     } 

    } 

    return (lines); 
} 


// Line 
private List<LatLng> decodePolyline(String encoded) { 

    List<LatLng> listPoints = new ArrayList<LatLng>(); 
    int index = 0, len = encoded.length(); 
    int lat = 0, lng = 0; 

    while (index < len) { 
     int b, shift = 0, result = 0; 
     do { 
      b = encoded.charAt(index++) - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b >= 0x20); 
     int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     lat += dlat; 

     shift = 0; 
     result = 0; 
     do { 
      b = encoded.charAt(index++) - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b >= 0x20); 
     int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     lng += dlng; 

     LatLng p = new LatLng((((double) lat/1E5)), (((double) lng/1E5))); 
     Log.i("Script", "POL: LAT: " + p.latitude + " | LNG: " + p.longitude); 
     listPoints.add(p); 
    } 
    return listPoints; 
} 

どのように適応するためのルート

public void drawRoute() { 
     PolylineOptions po; 
     if (this.polyline == null) { 
      po = new PolylineOptions(); 
      int i = 0; 

      for (int tam = list.size(); i < tam; ++i) { 
       po.add(list.get(i)); 
      } 

      po.color(Color.BLACK); 
      this.polyline = this.map.addPolyline(po); 
     } else { 
      this.polyline.setPoints(list); 
     } 

    } 

任意の提案を描きますコード?だから何が次であるあなたはすでにあなたのコードでは、原点と目的地のポイントを設定している

Image

答えて

1

: このコードは

は、私はこのような何かをする必要があり、ユーザーの場所やポイントの場所との間のルートを描く正常に動作しますそれらの間にウェイポイントを追加することです。 Hereは、ウェイポイントでURLを作成するためのコードの変更方法です。

そして、誰かがすでに複数のポイントを実装しているようです。here リンクからの関連コードスニペット。

Route.java

 private String makeURL (ArrayList<LatLng> points, String mode, boolean optimize){ 
     StringBuilder urlString = new StringBuilder(); 

     if(mode == null) 
      mode = "driving"; 

     urlString.append("http://maps.googleapis.com/maps/api/directions/json"); 
     urlString.append("?origin=");// from 
     urlString.append(points.get(0).latitude); 
     urlString.append(','); 
     urlString.append(points.get(0).longitude); 
     urlString.append("&destination="); 
     urlString.append(points.get(points.size()-1).latitude); 
     urlString.append(','); 
     urlString.append(points.get(points.size()-1).longitude); 

     urlString.append("&waypoints="); 
     if(optimize) 
     urlString.append("optimize:true|"); 
     urlString.append(points.get(1).latitude); 
     urlString.append(','); 
     urlString.append(points.get(1).longitude); 

     for(int i=2;i<points.size()-1;i++) 
     { 
      urlString.append('|'); 
      urlString.append(points.get(i).latitude); 
      urlString.append(','); 
      urlString.append(points.get(i).longitude); 
     } 


     urlString.append("&sensor=true&mode="+mode); 


     return urlString.toString(); 
} 
関連する問題