Overview: The application has a map in a Fragment, the Fragment retrieves the data for the markers from a Room Database and then draws the markers onto the map.
Issue: Whenever the markers are drawn onto the map, then the map fragment gets choppy and at points unresponsive to clicks, the lag of the map is fixed as soon as the markers are removed. I do not know why the markers are making it choppy and I suspect the issue to be related to loading the markers from the database but that's just a presumption.
The method which gets and draws the markers onto the map.
private void drawMarker(){
if (markersAreDrawn == false){
markerViewModel.getAllMarkers().observe(MapFragment.this, new Observer<List<MarkerObject>>() {
@Override
public void onChanged(List<MarkerObject> markerObjects) {
for (MarkerObject markerObject : markerObjects) {
if (checkedItems[0] == true && markerObject.getCategory().equals("Nature")) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(markerObject.getLatitude(), markerObject.getLongitude()))
.snippet(markerObject.getDescription())
.title(markerObject.getTitle()));
}
if (checkedItems[1] == true && markerObject.getCategory().equals("Abandoned")){
mMap.addMarker(new MarkerOptions()
.position(new LatLng(markerObject.getLatitude(), markerObject.getLongitude()))
.snippet(markerObject.getDescription())
.title(markerObject.getTitle()));
}
if (checkedItems[2] == true && markerObject.getCategory().equals("Camping")){
mMap.addMarker(new MarkerOptions()
.position(new LatLng(markerObject.getLatitude(), markerObject.getLongitude()))
.snippet(markerObject.getDescription())
.title(markerObject.getTitle()));
}
if (checkedItems[3] == true && markerObject.getCategory().equals("Sightseeing")){
mMap.addMarker(new MarkerOptions()
.position(new LatLng(markerObject.getLatitude(), markerObject.getLongitude()))
.snippet(markerObject.getDescription())
.title(markerObject.getTitle()));
}
}
}
});
} markersAreDrawn = true;
}
Then the drawMarker() method is executed in the OnMapReadyCallback which is called in the OnViewCreated().
I have looked into this issue a couple of times before but have not figured it out myself. Any and all help is very much appreciated.