用另一种方法删除折线

时间:2016-09-30 06:32:09

标签: android google-maps google-polyline

我一直在寻找如何删除折线,但没有一个解决了我的问题。我希望通过张贴这个我将得到所需的答案。 这里是。我创建了一个按钮,当我单击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);

                }

//单击按钮时调用此方法。      private void 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);
    }

单击该按钮时,文本将变为“停止”。它很成功但我希望我的折线也会消失。

1 个答案:

答案 0 :(得分:0)

在我的stopSending()方法中,我放了mMap.clear,这就是我解决问题的方法:)