Super slow marker adding - UI lags
Hi there, I am using MapBox Android SDK to place a pretty big amount of markers on a map. I'm adding markers one by one in real time on a dedicated thread, but each time an annotation is created on the map using the "mapbox.addMarker()" function, the UI lags for less than a second. I mean, it's not fluid like the map usually is.
The thing is that it'd be fine if only a couple of markers had to be added, but when an important number has to be displayed, it takes quite some time to get them all on the map (around a good minute for 418 annotations without any kind of customization.)
Could you share the code on how you are adding the marker to the map?
Thank you for the fast reply, here is my code to add markers to the map. It runs each time I'm pressing a dedicated button, and the goal would be to still be able to use the map without any lags while the markers are progressively added :
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final MapboxMap mapboxMap) {
mapboxMap.removeAnnotations();
Thread thread = new Thread() {
@Override
public void run() {
if (MyItems != null && MyItems.size() > 0) {
// MyItems is an ArrayList of HashMaps of items I want to display on my map with markers
// There I just go through each element of the ArrayList that has to be displayed
for (int i = 0; i < MyItems.size(); i++) {
// Just checking if my item has a location
if (MyItems.get(i).get("latitude") != null &&
MyItems.get(i).get("longitude") != null) {
// If everything seems fine, I create the marker and add it to the map
mapboxMap.addMarker(
new MarkerOptions()
.position(new LatLng(Double.parseDouble(MyItems.get(i).get("latitude")), Double.parseDouble(MyItems.get(i).get("longitude"))))
.title(MyItems.get(i).get("stuff1") + " ; " +
MyItems.get(i).get("stuff2") + " ; " +
MyItems.get(i).get("stuff3") + " ; " +
MyItems.get(i).get("stuff4"))
.snippet(MyItems.get(i).get("stuff6"))
);
}
}
// and there, just some notification that everything's finished
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Markers created", Toast.LENGTH_LONG).show();
}
});
}
}
};
thread.start();
}
});
Just a simple reminder that the current ArrayList only has 418 elements, so going through the whole list shouldn't take more than a second or two. Regardless of that, creating the markers in real time takes a minute, which is really long when GoogleMaps does the same in less than 5 seconds... And I can't even imagine if I do that on the full database (which contains around 1400 elements or so, and will grow bigger with some time)
If you have any clue about why it happens, let me know !
I had same problem. It drove me mad for a couple days. I was about switching to google maps :D
Like you, I used only map.addMarker() method. But when adding multiple markers, you should use map.addMarkers() method. Adding 400 markers shouldn't take more than 200ms.
Example:
ArrayList<MarkerOptions> markers = new ArrayList<>();
//add your new markers to array
for (int i = 0; i < MyItems.size(); i++) {
// Just checking if my item has a location
if (MyItems.get(i).get("latitude") != null &&
MyItems.get(i).get("longitude") != null) {
// If everything seems fine, I create the marker and add it to the map
//add to array, not to the map
markers.add(
new MarkerOptions()
.position(new LatLng(Double.parseDouble(MyItems.get(i).get("latitude")), Double.parseDouble(MyItems.get(i).get("longitude"))))
.title(MyItems.get(i).get("stuff1") + " ; " +
MyItems.get(i).get("stuff2") + " ; " +
MyItems.get(i).get("stuff3") + " ; " +
MyItems.get(i).get("stuff4"))
.snippet(MyItems.get(i).get("stuff6"))
);
}
}
//render
mMap.addMarkers(markers);
Hope this helps!
yay, it worked like a charm !! Indeed, creating a List of MarkersOptions and adding them to the map using addMarkers() made it work in less than a second, exactly what I was searching for ! I don't know what makes addmarker() so slow comparing to addmarkers() though, but eh. Now my map is super fast, that's cool !
Thank you so much for your fast answer~
Yes, the above solution works perfectly.