【问题标题】:Remove Polyline in another method用另一种方法删除折线
【发布时间】:2016-09-30 06:32:09
【问题描述】:

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

                }

//当按钮被点击时,这个方法被调用。 私人无效发送请求(){

        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);
    }

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

【问题讨论】:

    标签: android google-maps google-polyline


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 2013-02-10
      • 2020-08-02
      相关资源
      最近更新 更多