0

So I want a cirle around the users current location, but at the moment every time I move a new circle is created but the old one is still there. This results in a big mess on the map.

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setInterval(2000);
        locationRequest.setFastestInterval(1000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                if (locationResult == null) {
                    return;
                }

                mLastKnownLocation = locationResult.getLastLocation();
                checkLocation = mLastKnownLocation;

                    LatLng lastKnownLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());

                    Circle mapCircle = mMap.addCircle(new CircleOptions()
                            .center(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()))
                            .radius(20)
                            .strokeColor(Color.argb(200,0,64,122))
                            .strokeWidth(5)
                            .fillColor(Color.argb(100,82,189,236)));
                    mapCircle.setCenter(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()));

HERE I CALCULATE SOME OTHER STUFF

//                mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
            }
        };
        mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);

This is how I create the circle. Now I tried adding mapCircle.remove() but that resulted in not getting a cirle at all. How can I do this. Yes I already looked at this question: Remove a circle from Android Google Maps API v2 without clearing map and it doesn't seem to solve my problem.

1 Answers1

0

You could store local mapCircle in Activity or Fragment (which one you are using now) then just remove it before adding new one

class MyActivity extends Activity {
    Circle mapCircle = null;

    public someMethod() {
        // Inside location callback
        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                if (locationResult == null) {
                    return;
                }

                mLastKnownLocation = locationResult.getLastLocation();
                checkLocation = mLastKnownLocation;

                LatLng lastKnownLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());

                if (mapCircle != null) {
                    mapCircle.remove();
                }
                mapCircle = mMap.addCircle(new CircleOptions()
                        .center(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()))
                        .radius(20)
                        .strokeColor(Color.argb(200,0,64,122))
                        .strokeWidth(5)
                        .fillColor(Color.argb(100,82,189,236)));
                mapCircle.setCenter(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()));
            }
        }
    }
}

Tuan Luong
  • 3,902
  • 1
  • 16
  • 18