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



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

("正确">

的意思:阅读自动完成文本视图中的内容并正确执行"IF"条件(;

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

我的地图就绪代码:

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

这是我的按钮点击代码:

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

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

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

相关内容

  • 没有找到相关文章

最新更新