Unable to get GoogleMap OnMarkerClickListener to work

两盒软妹~` 提交于 2019-12-04 00:09:42
Shawn Lien

This works fine for me:

GoogleMap mMap;
Marker marker_1;

After initializing map,add a listener to it.

mMap.setOnMarkerClickListener((OnMarkerClickListener) this);

And call this

/**
 * handle marker click event
 */    
@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub
    if(marker.equals(marker_1)){
        Log.w("Click", "test");
        return true;
    }
        return false;           
}

If it returns "true", the click event is being handled properly.If you click a marker and return false it will just pop up the info window as usual.

According to Android documentation,the marker that's clicked returns true if the listener has consumed the event (i.e., the default behavior should not occur), false otherwise (i.e., the default behavior should occur). The default behavior is for the camera to move to the map and an info window to appear.

You can use this simply snipet:

import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;

GoogleMap mGoogleMap;
Marker marker_1;

In onMapReady(GoogleMap googleMap) add:

mGoogleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {

            // TODO Auto-generated method stub
            if(marker.equals(marker_1)){
                Log.w("Click", "test");
                return true;
            }
            return false;

        }
    });

Managed to get it to work by using the OnInfoWindowClickListener instead. Don't know why it wouldn't respond to the marker click though.

GoogleMap mMap;

use this:

mMap.setOnMarkerClickListener((OnMarkerClickListener) this);

and not mMap.setOnMarkerClickListener(OnMarkerClickListener);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!