第二次单击按钮时,我的折线不会消失

时间:2017-08-02 14:39:32

标签: android google-maps

我想要发生的是当我根据if条件在AutoCompleteTextView中输入内容时,当我单击按钮第一次时,程序将按照我的要求正确执行。

(“正确”的意思是:读取AutoCompleteTextView中的内容并正确地执行'IF'条件);

但是,当我第二次单击按钮将我在AutoCompleteTextView中输入的内容更改为另一个'IF'条件时,程序在传递第一个'if'条件时所做的标记和折线不会消失(不是被删除);这种情况发生无限次点击按钮,只有第一个按钮点击被程序正确完成/读取。

我的onMapReady代码:

 @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;

        //Position of Manila
        LatLng manila = new LatLng(14.5995, 120.9842);

        //adding details in Marker
             MarkerOptions option = new MarkerOptions();
             option.position(manila).title("Manila");

        //adding marker in map
             map.addMarker(option);
             map.moveCamera(CameraUpdateFactory.newLatLng(manila));

        //gotoLocation line
            gotoLocationZoom(14.5995, 120.9842, 15);
        //end gotoLocation line

        //routes
            apple();
            banana();
            cherry();
        //markerclick
            map.setOnMarkerClickListener(this);
    }

我的onMarkerClick代码:(但我认为这是无关紧要的,因为我没有点击,但我认为你可能需要尽可能多的代码)

@Override
    public boolean onMarkerClick(Marker marker) {
        tag = marker.getTag().toString();
        return false;
    }

这是我的onButtonClick代码:

public void onClick(View view){
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.text_category);

    if (textView.getText().toString().equals("apple")){

        Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();

        //remove Lines
            banana_line.remove();
            cherry_line.remove();
        //Remove Markers
            banana_marker.remove();
            cherry_marker.remove();

        //Show Apple Marker and lines
            apple();

    } else if (textView.getText().toString().equals("banana")){

        Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();
        //Remove Lines
            apple_line.remove();
            cherry_line.remove();

        //Remove markers
            apple_marker.remove();
            cherry_marker.remove();

        //Show banana Marker and lines
            banana();

    }else if (textView.getText().toString().equals("cherry")){

        Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();

        //Remove Lines
            apple_line.remove();
            banana_line.remove();

        //Remove Markers
            apple_Marker.remove();
            banana_Marker.remove();

        //Add cherry Marker and Lines
            cherry();

    } else{
        Toast.makeText(this, "Error no fruit like that", Toast.LENGTH_LONG).show();
    }

这是我的样本折线和标记代码:

public void apple() {

        LatLng apple = new LatLng(14.617071, 120.989945);

        //All about the Marker
            MarkerOptions appleMarkerOptions = new MarkerOptions();
            appleMarkerOptions.position(apple);
            appleMarkerOptions.title("Apple");
            appleMarkerOptions.snippet("So yummy");

            apple_marker = map.addMarker(appleMarkerOptions );
            apple_marker.setTag("Apple");

       //The Polylines
            apple_line= map.addPolyline(new PolylineOptions().add(
                new LatLng(14.617071, 120.989945),
                new LatLng(14.605693, 121.000863),
                new LatLng(14.605599, 121.000939),
                new LatLng(14.603097, 121.001786),
                new LatLng(14.602900, 121.001089),
                new LatLng(14.605246, 121.000252),
                new LatLng(14.613429, 120.992410),
                new LatLng(14.611934, 120.990768),
                new LatLng(14.617094, 120.985795)
                )
                        .width(5)
                        .color(Color.RED)

        );

public void banana(){

        LatLng banana = new LatLng(14.612056, 120.995471);

        //All about the Marker
            MarkerOptions bananaMarkerOptions = new MarkerOptions();
            bananaMarkerOptions .position(banana);
            bananaMarkerOptions .title("Banana");
            bananaMarkerOptions .snippet("I'm so yellow");

            banana_marker = map.addMarker(bananaMarkerOptions );
            banana_marker.setTag("Banana");

       //The Polylines
            banana_line = map.addPolyline(new PolylineOptions().add(
                    new LatLng(14.611319, 120.994752),
                    new LatLng(14.612855, 120.996404)
            )
                        .width(5)
                    .color(Color.YELLOW)
            );
        }

public void cherry(){
        //All about the Marker
            LatLng cherry = new LatLng(14.612056, 120.995471);
            MarkerOptions cherryMarkerOptions = new MarkerOptions();
            cherryMarkerOptions.position(cherry);
            cherryMarkerOptions.title("Cherry");
            cherryMarkerOptions.snippet("Cherry delight");

        //The Polylines
            cherry_marker= map.addMarker(cherryMarkerOptions );
            cherry_marker.setTag("Cherry");

            cherry_line = map.addPolyline(new PolylineOptions().add(
                    new LatLng(14.612128, 120.995578),
                    new LatLng(14.614495, 120.998217)
                    )
                            .width(5)
                            .color(Color.GRAY)
            );
}

1 个答案:

答案 0 :(得分:0)

我发现的解决方案是添加所有' _line.remove'在所有嵌套的if-else语句中。没有找到更容易/更短编码的任何其他解决方案。与此同时,我将把解决方案留在这里,直到我得到更好的解决方案。

//Remove lines
apple_line.remove();
banana_line.remove();
cherry_line.remove();
//Remove Markers
apple_marker.remove();
banana_marker.remove();
cherry_marker.remove();