Search code examples
androidnavigationskmaps

Enable automatic zoom with SKZoonLevelConfiguration


Here is the situation, I want to set up the navigation mode to adjust the zoom to route according to the current speed. See my code:

SKZoomLevelConfiguration[] zoomConfgs = new SKZoomLevelConfiguration[4];
zoomConfgs[0] = new SKZoomLevelConfiguration(0.0f, 10.0f, 15.0f);
zoomConfgs[1] = new SKZoomLevelConfiguration(10.0f, 20.0f, 13.0f);
zoomConfgs[2] = new SKZoomLevelConfiguration(20.0f, 60.0f, 10.0f);
zoomConfgs[3] = new SKZoomLevelConfiguration(60.0f, 100.0f, 8.0f);

SKNavigationSettings navSettings = new SKNavigationSettings();
navSettings.setNavigationType(SKNavigationSettings.SKNavigationType.REAL); 
navSettings.setNavigationMode(SKNavigationSettings.SKNavigationMode.CAR); 
navSettings.setDistanceUnit(SKMaps.SKDistanceUnitType.DISTANCE_UNIT_KILOMETER_METERS);
navSettings.setZoomLevelConfigurations(zoomConfgs);
navSettings.setPositionerVerticalAlignment(-0.25f);

SKNavigationManager.getInstance().setNavigationListener(navListener);
SKNavigationManager.getInstance().setMapView(surfaceView);
surfaceView.getMapSettings().setMapDisplayMode(SKMapSettings.SKMapDisplayMode.MODE_3D);
SKNavigationManager.getInstance().startNavigation(navSettings);
isNavigating = true;

So the fact here is the map isn't zooming when the navigation is activated. Probably i'm doing something wrong.


Solution

  • In 2.5 there was an issue with this API - it was fixed for 2.6.

    Here is a code snippet of how it works in 2.6:

        /** 
         * Launches a navigation on the current route 
         */ 
        private void launchNavigation() { 
            if (TrackElementsActivity.selectedTrackElement != null) { 
                mapView.clearTrackElement(TrackElementsActivity.selectedTrackElement); 
    
            } 
            // get navigation settings object 
            SKNavigationSettings navigationSettings = new SKNavigationSettings(); 
            // set the desired navigation settings 
            navigationSettings.setNavigationType(SKNavigationType.SIMULATION); 
            navigationSettings.setPositionerVerticalAlignment(-0.25f); 
            navigationSettings.setShowRealGPSPositions(false); 
    
            SKZoomLevelConfiguration[] configurations = new SKZoomLevelConfiguration[2]; 
            configurations[0] = new SKZoomLevelConfiguration(0, 70, 10); 
            configurations[1] = new SKZoomLevelConfiguration(70, 100, 15); 
            navigationSettings.setZoomLevelConfigurations(configurations); 
            // get the navigation manager object 
            SKNavigationManager navigationManager = SKNavigationManager.getInstance(); 
            navigationManager.setMapView(mapView); 
            // set listener for navigation events 
            navigationManager.setNavigationListener(this); 
    
            // start navigating using the settings 
            navigationManager.startNavigation(navigationSettings); 
            navigationInProgress = true; 
        } 
    

    Note: The speed values(km/h) set in SKZoomLevelConfigurations are correlated to the current speed(m/s) from SKNavigationState. The onUpdateNavigationState(SKNavigationState navigationState) method provides notifications when data related to the current speed is updated.