Clear markers from Google Map in Android - 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.

Related

Android Google Maps V2 Remove Single Marker

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();

How can I show and hide markers on google map in android using a button

I am new to android coding. I am trying to toggle on and off markers that I managed to display on my map, with a button in my action bar.
So far I have created this method, I do not understand what I need to do next
Here, basically I make an array of locations and use for loop to put all the markers on my map. Now what I want to be able to do is hide the markers if they are visible via a button click and show the markers if they are hidden.
public boolean showShops(){
rL = new ArrayList<LatLng>();
rl.add(new LatLng(40.433433, -1.422423));
rl.add(new LatLng(40.433434, -1.422534));
for(LatLng nRL : rL){
mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop")
}
return true;
}
I have been trying to figure it out for a long time now and cannot seem to find the solution. I have managed to find out that you have to setVisible(false); to hide and setVisible(true); to show, but I do not know how I can implement it. I tried adding that instead of .add in my above code but I get errors.
Can someone please help.
Thanks.
If there's nothing else on your map that you DON'T want to hide, use
clear on your GoogleMap object which removes all additional overlays of your map.
If that method doesn't fit you, you have to keep a reference to all markers (for example in an ArrayList) and call remove or setVisible() on each of them individually:
Keep an
ArrayList<Marker> myMarkers = new ArrayList<Marker>();
and also add every marker that you add to the map to that list.
for(LatLng nRL : rL){
myMarkers.add(mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop"));
}
If you want to set them all to invisible, iterate over that list and setVisible(false) on all of them.
for (Marker m : myMarkers) {
m.setVisible(false);
}
If you want to make your marker invisible you can also use marker.setAlpha(0).

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();

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