Benjamin Ting
Benjamin Ting

Reputation: 709

OsmDroidBonusPack OnMarkerClickListener not called

I have a strange issue with the OnMarkerClickListener in OsmBonusPack. The listener would not be called on click if I add the marker at OnCreate. However, if I add the marker after all initial set up, let say when the user interacts with the app, the OnMarkerClickListener would be called when the marker is clicked.

To illustrate, the OnMarkerClickListener added by this code will not be responsive:

@Override public void onCreate(Bundle savedInstanceState) {
    MapView mapView = (MapView) findViewById(R.id.map);
    mapView.setTileSource(TileSourceFactory.MAPNIK);

    Marker marker = new Marker(mapView);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setOnMarkerClickListener(MyOnMarkerClickListener);
    mapView.getOverlays().add(marker);
    mapView.invalidate();    
}

The OnMarkerClickListener added by this code will be responsive (I am even using the same marker and mapView reference):

public void markerAddedbyUser() {
    mapView.getOverlays().add(marker);
    mapView.invalidate();
}

I am wondering if it is related to the mapView.getOverlays() method not fully ready until the UI is completely drawn. However, I tried to put the first code in onCreateOptionMenu and it still doesn't work.

Upvotes: 1

Views: 404

Answers (1)

Benjamin Ting
Benjamin Ting

Reputation: 709

Shortly after I posted this question, I found the solution. I listed all the objects from MapView.getOverlays() using:

List<Overlay> overlays = mapView.getOverlays();
for(Overlay overlay : overlays) {
    System.out.println(overlay.getClass());
}

I found I have added a MapEventsOverlay some elsewhere and the overlay is at the top of all overlays (last item of the list). That MapEventsOverlay is avoiding the OnMarkerClickListeners to listen to click events. I solved the issue by adding the MapEventsOverlay to the first of the list:

mapView.getOverlays().add(0, mMapEventsOverlay);

Upvotes: 2

Related Questions