I am trying to draw a circle on the Google Map v2 in android with center as the current location as soon as the location gets changed. Now what I am seeing is, everytime when the location gets changed, circle keeps getting drawn(overlapping each other if the location is same) without deleting the previous circle. And same thing is happening with the Marker as well.
Below is the code I am using to draw the circle on the Google Map v2
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// Create a LatLng object for the current location
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
// Show the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(14));
CircleOptions circleOptions = new CircleOptions().center(latLng) // set center
.radius(1000) // set radius in meters
.fillColor(Color.TRANSPARENT) // default
.strokeColor(0x10000000).strokeWidth(5);
myCircle = map.addCircle(circleOptions);
map.addMarker(new MarkerOptions().position(latLng).title("You are here!"));
}
How do I make sure, that whenever the circle is getting drawn next time, previous circle and marker is cleared from the Google Map. What changes I need to make in my code?
Any help will be appreciated.
Removing things from the map is simple. For whatever reason, in GoogleMaps
v2 you cannot simply remove a Marker
by getting its id since that value is generated automatically on creation and thus pretty much useless. To work around this, all you need to do is create something that can store a reference to the object you want to remove. One way to do this is to create HashMap
and store a reference to your Marker
, circle, or anything else you want the ability to remove, with some unique id. By storing a reference to things you place on the map in a HashMap
however you can then call remove on the marker associated with that key each time your location updates. The same goes for the circle (though you will need a different HashMap
if you set the types as I have below - I don't know if you can use a generic String, Object map that will store both).
To use this method, declare your HashMap
like its an instance variable so it can be accessed from all the methods within your Activity
private HashMap<String, Marker> mapStuff = new HashMap<String, Marker>();
Then wherever you create your Marker
or other map objects, just add them to the HashMap
with some key value
Marker dude = map.addMarker(new MarkerOptions()
.position(newLatLng(latitude, longitude))
.title(MARKER_TITLE)
.snippet(SNIPPET));
mapStuff.put("userMarker", dude);
The concept is really the same as what was suggested by another poster, which is just to have a Marker
as an instance variable and store a reference there. Both do the same thing. The HashMap
approach works best if you're dealing with multiple markers or objects. If you're only dealing with one Marker
or one circle, the single variable approach is probably more correct since you do not need to use a Collection
to add or remove one marker. Just declare
private Marker userMarker;
and then where you add the Marker
to the map, store a reference
userMarker = map.addMarker(new MarkerOptions()
.position(newLatLng(latitude, longitude))
.title(MARKER_TITLE)
.snippet(SNIPPET));
When you update your location, probably in onLocationChanged
just check for the existance of the marker and remove it if present and re-add
if(userMarker == null){
displayUserMarker(location);
} else {
userMarker.remove;
displayUserMarker(location);
}