Android Google Maps V2 Remove Single Marker - android

I know this question has been asked previously But I did not understand that particular answer..there is an option of adding marker but no option for removing marker..
Please help me .. If you know
Also specify the coding for that one
Thanks

You can get the markers reference when you add it to the map. The marker class has a remove() method.
googleMap = fm.getMap();
Marker marker = googleMap.addMarker(..);
marker.remove();
If you want to clear all markers you can use the clear() method.
googleMap.clear();

Related

How to remove the Poly line from the Google Map v2 android

I am successfully making and drawing poly lines on a marker click event. But there is a thing happening which I do not want to be happen.
And that is when I click on marker Let say JackiMarker it draws route to the target location but then I click to second marker let say MichaelMarker it also draws the route but the previous marker is still there which I do not want to be.
I am sing this code to draw and remove the marker. It has the clearRoute function but it is not working for me , please help me what to do . Please help me in removing the last polyline I have drawn on map.
Please try this,
Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);
Then when you want to remove it:
polyline.remove();
If you have lots of Polylines, just add them to a List as they are put on the map:
List<Polyline> polylines = new ArrayList<Polyline>();
for(....)
{
polylines.add(this.mMap.addPolyline(new PolylineOptions()....));
}
And when you want to delete:
for(Polyline line : polylines)
{
line.remove();
}
polylines.clear();
The key is to keep a reference to the Polyline objects and call .remove() on each one.
It is 100 % working code which I shared in the question , the problem was in my end .
what I was doing , was trying to delete the path when clicked on the marker and on the OnMarkerClickListener I was initiating the Route Class , which created another instance of the same class thus creating the array of polylines from 0 index which Is why i was not getting into the for loop of the clearRoute method.
so by instantiating the Route class as a global solved my problem.

How to remove a particular marker from Google Map in Android

I add a marker on each map click, and I only want to see the last one.
I tried the following code:
if(marker!=null)
{
marker.visible(false);
marker=null;
}
marker=new MarkerOptions().position(latLng);
googleMap.addMarker(marker);
I see every marker even though I set it to invisible.
How can I remove after each click? I can't find any remove methods.
Just keep a reference to the marker each time you do the click :
Marker marker = map.addMarker(MARKER_OPTIONS);
Then call remove() method :
marker.remove();
As mention here in documentation , Remove() method in Marker class will help you
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove()
and here tutorial about adding and removing markers on map
http://www.jiahaoliuliu.com/2013/08/android-adding-and-removing-markets-on.html

how to remove MarkerOptions objects in google api v2?

I added some Markers in my map and now I want to remove them. Is there any method that I can use to remove my Marker? I used MarkerOptions class to build my Markers and add() method of GoogleMap to add them. And I have a reference of all of my Markers in a List.
I really need your help.
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method:
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove
Removes this marker from the map. After a marker has been removed, the behavior of all its methods is undefined.
marker.remove()
It will remove all the markers. use this one to remove particular one
for(Marker m : marker)
m.remove();

Clear markers from Google Map in Android

I have added map on fragment activity and added several marker using addMarker function, but i am able to remove all markers , I am getting notification for different list of markers,
Now i wants to remove all markers and add new one.
one way to keep all markers in list and remove one by one, (marker.remove())
Is there any better way to clear all marker.
If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.
If you do not wish to clear polylines and only the markers need to be removed follow the steps below.
First create a new Marker Array like below
List<Marker> AllMarkers = new ArrayList<Marker>();
Then when you add the marker on the google maps also add them to the Marker Array (its AllMarkers in this example)
for(int i=0;i<places.length();i++){
LatLng location = new LatLng(Lat,Long);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title("Your title");
Marker mLocationMarker = Map.addMarker(markerOptions); // add the marker to Map
AllMarkers.add(mLocationMarker); // add the marker to array
}
then finally call the below method to remove all markers at once
private void removeAllMarkers() {
for (Marker mLocationMarker: AllMarkers) {
mLocationMarker.remove();
}
AllMarkers.clear();
}
call from anywhere to remove all markers
removeAllMarkers();
I found this solution when i was looking for a way to remove only the map markers without clearing the polylines. Hope this will help you too.

Google maps API v2 find Marker by id

You can simply add Markers with the new V2 Google maps API on Android. Because the Markers are recreated on configuration change or on save instance, you have to reference them via its ids. The question is how to remove from map Marker with particular id?
My use case is to add Markers to the map, store its id's with mapping to real objects. Then user removes one of this real objects, so I find the Marker id and want to remove Marker from the map and the only way I know about is to have Marker object and call remove() on it.
The documentation is wrong about recreating markers on configuration change and it's actually good for us it is wrong there.
I have also came across similar situation in doing map clustering, where i need to remove the marker when it is added to an cluster.
The solution which i used is that, i am holding the reference to the markers when they are being created and added into the map and store the marker in a Map (String - Marker) ,where key(String) would be an auto-generated marker id, and value would be the marker object.
Now,you can get the reference to the marker object by its id and call remove() on that marker.
I hope this will be helpful to you.
SImply try the following:
private Marker myMarker;
myMarker = getMap().addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!"));
now the marker you want to remove you can call the
myMarker.remove();

Categories

Resources