Unable to get GoogleMap OnMarkerClickListener to w

2020-07-02 10:12发布

Using V2 maps.

Got it all basically working, including adding of markers.

However, I need to take action when the marker is tapped, so I've installed a handler:

thisMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override public boolean onMarkerClick(Marker marker) {
        //  Take some action here
        return true;
    }

}
);

Only problem is - onMarkerClick() never gets called at all. Cannot see why - tried creating the listener when the map was originally created and also after the marker is put in place, no difference.

?

4条回答
欢心
2楼-- · 2020-07-02 10:43

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

查看更多
女痞
3楼-- · 2020-07-02 10:51

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.

查看更多
一夜七次
4楼-- · 2020-07-02 10:59

GoogleMap mMap;

use this:

mMap.setOnMarkerClickListener((OnMarkerClickListener) this);

and not mMap.setOnMarkerClickListener(OnMarkerClickListener);

查看更多
Summer. ? 凉城
5楼-- · 2020-07-02 11:02

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;

        }
    });
查看更多
登录 后发表回答