1
votes

I'm adding some number of markers (varies, maybe 5-20) to a mapbox map in a loop as so:

Marker newMarker = mainMap.addMarker(new MarkerOptions()
                                        .position(new LatLng(lat, lng))
                                        .title(name)
                                        .icon(icon)
                                        .snippet("icon"));

Through my testing, I know this above code is being ran for each marker.

I'm also adding the onMarkerClickListener in the onMapReady function as so:

@Override
public void onMapReady(MapboxMap mapboxMap) {
    this.mainMap = mapboxMap;
    requestLocationPermissions();
    mainMap.setOnMarkerClickListener(this);
}

The problem I'm facing is that only the first marker added actually responds to clicks, the others are drawn on the map but do nothing.

I've noticed however if I minimize the app, and then go back to it, some, or sometimes all of the icons (but only sometimes) become clickable. When the app is minimized, the following is output to the run console:

V/FA: Recording user engagement, ms: 43419
  Connecting to remote service
V/FA: Activity paused, time: 155301902
D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=43419, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=2134823340813427911}]
V/FA: Connection attempt already in progress
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 2

Does anyone have any idea why this is happening? I'm guessing it has something to do with onPause / onResume behavior, and whatever is ran when the fragment containing the mapbox map is resumed. How do I fix this behavior so all markers are always clickable?

1

1 Answers

4
votes

I tried to reproduce your error with this code and com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.13.0:

public void onMapReady(MapboxMap mapboxMap) {
    // ...

    mapboxMap.setOnMarkerClickListener(this);

    for(int i = 0; i < 10; i++) {
        locations.add(new LatLng(10.683 + ((double) i/100), 53.874236));
        Log.d(TAG, "Add Marker: " + locations.get(locations.size() - 1));
        mapboxMap.addMarker(new MarkerOptions()
                .position(locations.get(locations.size() - 1))
                .title("Marker: " + i)
                .snippet("Snipped"));
    }

    LatLngBounds latLngBound = new LatLngBounds.Builder()
            .include(locations.get(0))
            .include(locations.get(locations.size()-1))
            .build();
    mapboxMap.easeCamera(CameraUpdateFactory.newLatLngBounds(latLngBound, 200), 1000);
}

@Override
public boolean onMarkerClick(@NonNull Marker marker) {
    Log.d(TAG, "onMarkerClick: " + marker.getTitle());
    return true;
}

If I don't change the mapview with rotation/zoom I confirm your observed behavior. But after zooming in I could click a marker. I guess you should post this issue here