Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Polygons from Google Map

I've drawn polygons on map whenever user drag on google map. Now, i want to reset map on a button click and need to remove all polygons which map object have. I've wrote code for remove but not working. As you can see, I've making list of polygons by polygonList.add(polygon); and on btn_reset_map click I've deleted all items also clear the map but when i again drag on map, the all previous polygons appears again that is my problem. Kindly tell how to remove all polygons permanentrly on btn_reset_map click.

Maps layout:

<?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>

Code:

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

        }
        });
like image 482
zohaib Avatar asked Aug 18 '26 16:08

zohaib


1 Answers

maybe your problem is remove() function . the remove that you called is from arrayList and you have to call remove() from polygone object like this :

for(Polygone poly : polygonList){
   poly.remove();
}
like image 162
Kapta Avatar answered Aug 20 '26 04:08

Kapta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!