androidmapboxgeojson

Mapbox for Android: Change map input based on current zoom level


I am adding a large amount of GeoJSON data into Mapbox in an Android app. This makes scrolling through the map very slow on higher zoom levels. For this reason I would like to change the input data based on the current zoom level. I looked for a function like getCurrentZoomlevel(), but all I could find was a getMaxZoomLevel() function in the MapView class and this only gives my the maximum possible zoom level. Is there a function that I can call which gives me the current zoom level?

For this solution to work, I would also need to remove and add certain polygons dynamically based on the zoom level. Is it possible to remove polygons without reloading the entire map?


Solution

  • Starting with 4.0.0 of the Mapbox Android SDK, interaction with the map happens using the MapboxMap object, not the MapView. It sounds like you are trying to add a zoom listener which doesn't exist but a onCameraChangeListener does and from that you can check the zoom level.

    mapboxMap.setOnCameraChangeListener(new MapboxMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition position) {
    
            ...
    
            if (position.zoom < 12) {
    
                ...
    
            }
        }
    });
    

    If your GeoJSON file is large and you are trying to draw polygons/polylines you might want to have a look at the Style API we will be introducing in 4.2.0. It allows you to add geojson layers and style them. If you are interested I'd recommend taking a look at the examples found in the Mapbox Android Demo App. This Github issue shows off a bunch of the examples I added.

    EDIT: forgot to mention to remove polygons or polylines from the map you have a few options. Either use mapboxMap.removeAnnotations(); to remove all annotations from the map or mapboxMap.removePolygon(); to remove a polygon for example. removePolygon takes in a Polygon object which you'll need to assign when adding the polygon to the map:

    Polygon polygon = map.addPolygon(new PolygonOptions()
                            .addAll(<List of points making up polygon>)
                            .setFillColor(color));