每当用户在Google地图上拖动时,我都会在地图上绘制多边形。现在,我想在单击按钮时重置地图,并且需要删除地图对象拥有的所有多边形。我已经写了删除代码,但是没有用。如您所见,我已经通过polygonList.add(polygon);
制作了多边形列表,并在btn_reset_map
单击,删除了所有项目,同时清除了地图,但是当我再次在地图上拖动时,所有先前的多边形再次出现那是我的问题。请告诉我们如何在btn_reset_map
单击时永久删除所有多边形。
地图布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<FrameLayout
android:id="@+id/fram_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btn_draw_State"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Free Draw" />
<Button
android:id="@+id/btn_reset_map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</FrameLayout>
</FrameLayout>
代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
FrameLayout fram_map = (FrameLayout) findViewById(R.id.fram_map);
Button btn_draw_State = (Button) findViewById(R.id.btn_draw_State);
Button btnReset = (Button) findViewById(R.id.btn_reset_map);
btn_draw_State.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Is_MAP_Moveable = !Is_MAP_Moveable;
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mMarkerPoints!=null) {
mMarkerPoints.clear();
for(int i = 0; i<polygonList.size(); i++) {
polygonList.remove(i);
}
polygonList.clear();
mMap.clear();
}
}
});
fram_map.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
Double latitude, longitude;
int x_co = Math.round(x);
int y_co = Math.round(y);
Projection projection = mMap.getProjection();
Point x_y_points = new Point(x_co, y_co);
LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
latitude = latLng.latitude;
longitude = latLng.longitude;
Log.d("latlong_", latitude+", "+longitude);
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
Log.d("latlong_", "down "+latitude+", "+longitude);
// finger touches the screen
mMarkerPoints.add(new LatLng(latitude, longitude));
case MotionEvent.ACTION_MOVE:
// finger moves on the screen
Log.d("latlong_", "move "+latitude+", "+longitude);
mMarkerPoints.add(new LatLng(latitude, longitude));
break;
case MotionEvent.ACTION_UP:
PolygonOptions rectOptions = new PolygonOptions();
rectOptions.addAll(mMarkerPoints);
rectOptions.strokeColor(Color.RED);
rectOptions.strokeWidth(5);
rectOptions.fillColor(Color.parseColor("#40c4c4c4"));
Polygon polygon = mMap.addPolygon(rectOptions);
polygonList.add(polygon);
break;
}
return Is_MAP_Moveable;
}
});
答案 0 :(得分:1)
也许您的问题是remove()
函数。您调用的remove是来自arrayList,您必须像这样从多边形对象调用remove():
for(Polygone poly : polygonList){
poly.remove();
}