永不在模拟器中调用onMarkerClick()

时间:2018-08-03 14:02:27

标签: android

我想向GoogleMap注册一个OnMarkerClickListener:

  • 实现GoogleMap.OnMarkerClickListener接口
  • 通过GoogleMap.setOnMarkerClickListener(this)注册侦听器

但是当我单击一个标记时,永远不会调用onMarkerClick()回调。为了进行测试,我还实现了OnMapLongClickListener,并且效果很好。

public class ShowCoords extends FragmentActivity implements
    OnMapReadyCallback,
    LocationListener,
    GoogleMap.OnCameraMoveListener,
    GoogleMap.OnCameraMoveStartedListener,
    GoogleMap.OnMyLocationButtonClickListener,
    GoogleMap.OnMyLocationClickListener,
    GoogleMap.OnMarkerClickListener,
    GoogleMap.OnMapLongClickListener {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.show_coords);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setScrollGesturesEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setMapToolbarEnabled(false);

    if (gpsPermission()) {
        mMap.setMyLocationEnabled(true);
    }
    mMap.setOnMyLocationButtonClickListener(this);
    mMap.setOnMyLocationClickListener(this);
    mMap.setOnCameraMoveStartedListener(this);
    mMap.setOnCameraMoveListener(this);

    mMap.setOnMarkerClickListener(this);

    mMap.setOnMapLongClickListener(this);

    if (displayFormat.equals(getString(R.string.heatmapValue))) {
        // display markers as heatmap
    } else if (displayFormat.equals(getString(R.string.clusterValue))) {
        // diplay markers as cluster 
        mClusterManager = new ClusterManager<>(this, mMap);
        mMap.setOnCameraIdleListener(mClusterManager);
        // this causes the problem
        mMap.setOnMarkerClickListener(mClusterManager);

        for (LatLng p : coords) {
            mClusterManager.addItem(new MyClusterItem(p));
        }
    } else {
        Log.w(TAG, "onMapReady: coords still null, nothing to display");
    }

    //just for testing
    mMap.addMarker(new MarkerOptions().position(new LatLng(51.866404, 12.643411)));
}

@Override
public boolean onMarkerClick(Marker marker) {        
    Log.i(TAG, "onMarkerClick");
    return true;
}

@Override
public void onMapLongClick(LatLng latLng) {
    Log.i(TAG, "onMapLongClick");
}

// removed other callbacks
}

任何想法,怎么了?

1 个答案:

答案 0 :(得分:0)

我明白了。当我将标记显示为群集时(通过Google Maps Android Marker Clustering Utility),clustermanager注册为OnMarkerClickListener,而我的侦听器的注册也丢失了。解决方法是,我省略了clustermanager的注册,并由我自己将click事件传递给了clustermanager。