Reputation: 3905
Is there a way of calling onClick event of a specific marker manually (without physically tapping the marker)?
Upvotes: 16
Views: 17010
Reputation: 8467
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()
}
}
}
Upvotes: 0
Reputation: 5984
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
.
Upvotes: 1
Reputation: 770
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
Upvotes: 0
Reputation: 1874
No, but you can simulate the onClick event. 2 things happen when you click a marker:
The above can be achieved with 2 lines of code:
marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);
Upvotes: 12
Reputation: 7929
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');
Upvotes: 2
Reputation: 355
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.
Upvotes: 1
Reputation: 3687
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) {}
Upvotes: 4