Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set click listener on marker in google map?

i have two marker in example where one is for current location and one is for destination i have drawn a Route path between them but now in same example i want to set a click listener on both the marker can you suggest some good example related this ..?

like image 923
haresh Avatar asked May 15 '17 08:05

haresh


3 Answers

Here is how to do it using Kotlin:

private lateinit var mMap: GoogleMap  //declaration inside class

override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap
    mMap.setOnMarkerClickListener { marker ->
        if (marker.isInfoWindowShown) {
            marker.hideInfoWindow()
            } else {
            marker.showInfoWindow()
            }
        true
        }
}

Hope this helps some beginner like me.

like image 191
Dennis Avatar answered Oct 11 '22 14:10

Dennis


This is how you would go about adding an onClick listener for markers on a map:

GoogleMap mMap;
Marker marker = mMap.addMarker(
   new MarkerOptions()
       .position(new LatLng(dLat, dLong))
       .title("Your title")                                       
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin)));

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker m) {}
}
like image 37
Noel Omondi Avatar answered Oct 11 '22 15:10

Noel Omondi


  1. Let your class/fragment implement OnMarkerClickListener
  2. Register marker click callback to your googleMap with googleMap.setOnMarkerClickListener(this); in your setup
  3. Override the onMarkerClick

    @Override
    public boolean onMarkerClick(final Marker marker) {
        //handle click here      
    }
    
like image 21
Denny Avatar answered Oct 11 '22 15:10

Denny