Search code examples
androidgoogle-mapsandroid-roomgoogle-maps-markerslag

Adding markers onto the map makes the map lag


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.


Solution

  • Indeed map lags with large amount of markers on it, but it should be thousands of markers on it. Probably it is because you add markers to same places several times. Try to clean map before drawing markers. Something like that:

    ...
    @Override
    public void onChanged(List<MarkerObject> markerObjects) {
        <YOUR_GOOGLE_MAP_OBJECT_NAME>.clear();
        for (MarkerObject markerObject : markerObjects) {
    ...
    

    Or better save marker to list like in this answer of Hesam and manage it (add only new markers, etc.).