2017-02-02 79 views
0

我的應用程序,我在地圖上添加2標記。當我點擊添加第二個標記時,我調用一個函數來計算路線並在地圖上添加折線。 這是折線安卓刪除折線或替換

protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points = null; 
     PolylineOptions lineOptions = null; 
     MarkerOptions markerOptions = new MarkerOptions(); 
     String distance = ""; 
     String duration = ""; 
     String value_duration = ""; 


     // Traversing through all the routes 
     for (int i = 0; i < result.size(); i++) { 
      points = new ArrayList<LatLng>(); 
      lineOptions = new PolylineOptions(); 

      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 

      // Fetching all the points in i-th route 
      for (int j = 0; j < path.size(); j++) { 
       HashMap<String, String> point = path.get(j); 


       if (j == 0) { // Get distance from the list 
        distance = (String) point.get("distance"); 
        continue; 
       } else if (j == 1) { // Get duration from the list 
        duration = (String) point.get("duration"); 
        value_duration = (String) point.get("duration_"); 
        continue; 
       } 

       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 

       points.add(position); 
      } 

      // Adding all the points in the route to LineOptions 
      lineOptions.addAll(points); 
      lineOptions.width(10); 
      lineOptions.color(Color.parseColor("#F7B907")); 

     } 

     // Drawing polyline in the Google Map for the i-th route 
     distanza_percorso = distance; 
     durata_percorso = duration; 

     testo_car.setText(durata_percorso); 

     durata_tragitto = Float.parseFloat(value_duration); 
     Log.d("durata_tragitto", "durata: " + value_duration); 

     polylineFinal = map.addPolyline(lineOptions); 


     moveToBounds(map.addPolyline(lineOptions)); 


    } 

現在我想通過地圖,如果我更換第二標記,並調用一個新的計算路線刪除折線的代碼。 我寫這個編碼:

if (polylineFinal != null){ 
      polylineFinal.remove(); 
     } 

但地圖上的折線不會被刪除。 你能幫我嗎?任何想法爲什麼?

感謝

回答

1

不知道,但嘗試這個更新添加折線代碼,

polylineFinal = map.addPolyline(lineOptions); 
moveToBounds(polylineFinal); 
+0

感謝您的重播和幫助。現在工作正常:-) – APPGIS