2016-09-30 69 views
0

我一直在尋找如何去除折線,但沒有一個解決了我的問題。我希望通過張貼這個我會得到所需的答案。 這是。我創建了一個按鈕,其中當我點擊我的btnFindPath折線顯示,這表明我現在就開始駕駛以另一種方法刪除折線

btnFindPath = (Button) findViewById(R.id.btnStartStop); 
     btnFindPath.setTag(1); 
     btnFindPath.setText("Start"); 
     btnFindPath.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final int status = (Integer) v.getTag(); 
       if (status == 1) { 

        Date date = new Date(mLastLocation.getTime()); 
        DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext()); 
        timeStarted = dateFormat.format(date); 
        sendRequest(); 
        buildGoogleApiClient(); 
        btnFindPath.setText("Stop"); 
        v.setTag(0); 
       } else { 
        dialog(); 
        v.setTag(tag); 

       } 

//當按鈕點擊這個方法被調用。 私人無效sendRequest將(){

 String origin = etOrigin.getText().toString(); 
     String destination = etDestination.getText().toString(); 
     String mode = "mode=transit"; 
     if (origin.isEmpty()) { 
      Toast.makeText(this, "Please enter origin address!", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     if (destination.isEmpty()) { 
      Toast.makeText(this, "Please enter destination address!", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     try { 
      new DirectionFinder(this, origin, destination, mode).execute(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 


    } 

@Override 
    public void onDirectionFinderStart() { 
     progressDialog = ProgressDialog.show(this, "Please wait.", 
       "Finding direction..!", true); 

     if (originMarkers != null) { 
      for (Marker marker : originMarkers) { 
       marker.remove(); 
      } 
     } 

     if (destinationMarkers != null) { 
      for (Marker marker : destinationMarkers) { 
       marker.remove(); 
      } 
     } 

     if (polylinePaths != null) { 
      for (Polyline polyline : polylinePaths) { 
       polyline.remove(); 
      } 
     } 
    } 

public void onDirectionFinderSuccess(List<Route> routes) { 
     progressDialog.dismiss(); 
     polylinePaths = new ArrayList<>(); 
     originMarkers = new ArrayList<>(); 
     destinationMarkers = new ArrayList<>(); 

     for (Route route : routes) { 
      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16)); 
      ((TextView) findViewById(R.id.tvDuration)).setText(route.duration.text); 
      ((TextView) findViewById(R.id.tvDistance)).setText(route.distance.text); 
      originMarkers.add(mMap.addMarker(new MarkerOptions() 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue)) 
        .title(route.startAddress) 
        .position(route.startLocation))); 
      destinationMarkers.add(mMap.addMarker(new MarkerOptions() 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green)) 
        .title(route.endAddress) 
        .position(route.endLocation))); 

      polylineOptions = new PolylineOptions(). 
        geodesic(true). 
        color(Color.BLUE). 
        width(10); 

      for (int i = 0; i < route.points.size(); i++) 
       polylineOptions.add(route.points.get(i)); 

      polylinePaths.add(mMap.addPolyline(polylineOptions)); 
    } 
} 

public void dialog() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure you want to end driving?") 
       .setCancelable(false) 
       .setPositiveButton("YES", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         btnFindPath.setText("Start"); 
         tag = 1; 
         stopSending(); 
        } 
       }) 
       .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alertDialog = builder.create(); 
     alertDialog.show(); 
    } 
// when the dialog message was "yes" this method was called 
    private void stopSending() { 

     // this is where I want my code in deleting polyline be putted. 

     // this is my code but it doesn't work still my polyline was their 
     polylinePaths.remove(polylineOptions); 
    } 

當按鈕被點擊的文字將變爲「停止」。這是成功的,但我希望我的折線也會消失。

回答

0

在我的stopSending()方法中,我把mMap.clear,這就是我如何解決我的問題:)

相關問題