I have a problem with using Google maps API on Android. I have a button which removes the selected marker off the maps interface, and information about it from the SQLite db that I have set up. Although my only problem is once multiple markers are on the map this feature stops and doesn't remove them:
Below shows the method which removes the marker from the maps and replaces them. As I said this works perfectly fine with a single marker and my testing has been a success, but with multiple it does not work.
I have an onclicklisterner for the markers which displays the information and a popup box for the marker, for the user to remove the marker they must click the marker which sets the global variable to that object then once the remove button is pressed the removeMarker() method is called. The getAllMarkers() method loops through a SQLite db and pulls information and adds to the maps.
End problem: Removing a marker when multiple markers are placed on the map doesn't work. Only works when a single marker is placed on the map.
Marker lastOpened = null;
To remove the information from the db the condition in the if statement returns a boolean value if it has been done:
if(this.mDbHelper.deleteLine(lastOpened.getTitle()))
Remove method
public void removeMarker(){
if(this.lastOpened != null){
if(this.mDbHelper.deleteLine(lastOpened.getTitle())){
lastOpened.remove();
getAllMarkers();
}
}
}
Thanks
You can either use googleMap.clear() or you can store your Markers in a collection of kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
Here's a related ticket discuss how to remove marker: Remove a marker from a GoogleMap
Is there a way of calling onClick event of a specific marker manually (without physically tapping the marker)?
No, but you can simulate the onClick event. 2 things happen when you click a marker:
The info window for the corresponding clicked marker is shown.
The camera pans to the marker.
The above can be achieved with 2 lines of code:
marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);
Try this ,
Implement marker click listener from your map class ,
public class MapView extends FragmentActivity implements OnMarkerClickListener{}
it will override onMarkerClickEvent as follows ,
#Override
public boolean onMarkerClick(final Marker marker) {}
NO, you can't triger a marker click event directly (from code).
You can just use mMap.setOnMarkerClickListener(...);, to handle markers click event.
But there is an alternative if you use your map in WebView, so you can trigger a marker click event with JavaScript:
//In V2 version:
GEvent.trigger(markers[i], 'click');
//In V3 version:
google.maps.event.trigger(markers[i], 'click');
The answer is no. You can't set the onClick of a particular marker separately.
However , using Map.setOnMarkerClickListener(_) you can set a listener for all such events. You should be able to retrieve the marker object in the listener called whenever any marker is clicked . You can use some identification to see if this is the particular marker you desire and act accordingly.
The identification could be any of the properties specific to that marker , title being the obvious choice. However, you can filter markers using any desired property.
I just stumbled across this and wasn't helped by the answers. So for future readers - if you are adding a map.setOnMarkerClickListener(yourClickHandler), then it's quite straight forward.
Abstract the logic from yourClickHandler and keep a reference to all the markers... I.e.
private val markers = arrayListOf<Marker>()
Wherever you add your markers to the map, also add them to your markers array. I.e. something like
val marker = MarkerOptions().position(...).......
markers.add(map.addMarker(marker))
And yourClickHandler would look something like
val yourClickHandler = GoogleMap.OnMarkerClickListener {
markerClickHandler(marker = it)
return#OnMarkerClickListener false
}
Now, whenever you press a marker on the map, yourClickHandler will call markerClickHandler() and whatever you do in there will happen. Also, when you wan't to press a marker programmatically, simply pass that marker to markerClickHandler.
You CAN simulate a marker click. Create your MyMarkerManager class extending from MarkerManager class.
The class has a function onMarkerClick() which you can call manually to simulate the event.
For more details refer this link.
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/MarkerManager.java
The GoogleMap object has a method Marker addMarker(mk: MarkerOptions) that returns a proper Marker instead of MarkerOptions.
So as soon as you add it you can simulate click behavior as follows:
fun addAndZoom(mk: MarkerOptions, needsHighlight: Boolean) {
mapView.getMapAsync { map ->
val actualMarker = map.addMarker(mk)
if(needsHighlight) {
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(mk.position, 14F)
map.animateCamera(cameraUpdate)
actualMarker.showInfoWindow()
}
}
}
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).
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
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();