java - How to clear the circle and marker from the Google Map v2 in android -
i trying draw circle on google map v2 in android center current location location gets changed. seeing is, everytime when location gets changed, circle keeps getting drawn(overlapping each other if location same) without deleting previous circle. , same thing happening marker well.
below code using draw circle on google map v2
@override public void onlocationchanged(location location) { if (location != null) { // create latlng object current location latlng latlng = new latlng(location.getlatitude(), location.getlongitude()); // show current location in google map map.movecamera(cameraupdatefactory.newlatlng(latlng)); // zoom in 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 here!")); } how make sure, whenever circle getting drawn next time, previous circle , marker cleared google map. changes need make in code?
any appreciated.
removing things map simple. whatever reason, in googlemaps v2 cannot remove marker getting id since value generated automatically on creation , pretty useless. work around this, need create can store reference object want remove. 1 way create hashmap , store reference marker, circle, or else want ability remove, unique id. storing reference things place on map in hashmap can call remove on marker associated key each time location updates. same goes circle (though need different hashmap if set types have below - don't know if can use generic string, object map store both).
to use method, declare hashmap instance variable can accessed methods within activity
private hashmap<string, marker> mapstuff = new hashmap<string, marker>(); then wherever create marker or other map objects, add them hashmap key value
marker dude = map.addmarker(new markeroptions() .position(newlatlng(latitude, longitude)) .title(marker_title) .snippet(snippet)); mapstuff.put("usermarker", dude); the concept same suggested poster, have marker instance variable , store reference there. both same thing. hashmap approach works best if you're dealing multiple markers or objects. if you're dealing 1 marker or 1 circle, single variable approach more correct since not need use collection add or remove 1 marker. declare
private marker usermarker; and add marker map, store reference
usermarker = map.addmarker(new markeroptions() .position(newlatlng(latitude, longitude)) .title(marker_title) .snippet(snippet)); when update location, in onlocationchanged check existance of marker , remove if present , re-add
if(usermarker == null){ displayusermarker(location); } else { usermarker.remove; displayusermarker(location); }
Comments
Post a Comment