Sorry, you need to enable JavaScript to visit this website.

Zooming the route

Zooming the route

Zooming in and out are used to navigate through map areas that are too large or too small to be conveniently displayed within a single window. While following the chevron, zooming alters the map scale because it changes the proportion of the workspace shown in each window.

Sample use case: You are a taxi driver navigating through the city of Łódź, and thanks to the zoom level adjusted to the velocity of your car you see a convenient map area for you to move.

Use the following code snippets to try this in your app:

fun setupLocationUpdateListener(tomtomMap: TomtomMap) {
    tomtomMap.addLocationUpdateListener { location ->
        updateZoomLevelBaseOnNewLocation(tomtomMap, location)
    }
}

private fun updateZoomLevelBaseOnNewLocation(tomtomMap: TomtomMap, location: Location) {
    val zoomLevel = when (location.speed) {
        in SMALL_SPEED_RANGE_IN_KMH -> SMALL_SPEED_ZOOM_LEVEL
        in MEDIUM_SPEED_RANGE_IN_KMH -> MEDIUM_SPEED_ZOOM_LEVEL
        in GREATER_SPEED_RANGE_IN_KMH -> GREATER_SPEED_ZOOM_LEVEL
        in BIG_SPEED_RANGE_IN_KMH -> BIG_SPEED_ZOOM_LEVEL
        in HUGE_SPEED_RANGE_IN_KMH -> HUGE_SPEED_ZOOM_LEVEL
        else -> tomtomMap.zoomLevel
    }
    if (tomtomMap.zoomLevel != zoomLevel) {
        setCameraPosition(tomtomMap, zoomLevel = zoomLevel)
    }
}

private fun setCameraPosition(tomtomMap: TomtomMap, zoomLevel: Double) {
    if (!isZooming) {
        isZooming = true
        tomtomMap.centerOn(
            CameraPosition.builder()
                .zoom(zoomLevel)
                .animationDuration(ZOOM_CHANGE_ANIMATION_MILLIS)
                .build(),
            onMapCenteredCallback
        )
    }
}

private val onMapCenteredCallback = object : OnMapCenteredCallback {
    override fun onFinish() {
        isZooming = false
    }

    override fun onCancel() {
        isZooming = false
    }
}

Lookup for the constants used in the sample code:

private const val EXAMPLE_START_ZOOM_LEVEL = 19.0
private const val ZOOM_CHANGE_ANIMATION_MILLIS = 300
private const val SMALL_SPEED_ZOOM_LEVEL = 19.0
private const val MEDIUM_SPEED_ZOOM_LEVEL = 18.0
private const val GREATER_SPEED_ZOOM_LEVEL = 17.0
private const val BIG_SPEED_ZOOM_LEVEL = 16.0
private const val HUGE_SPEED_ZOOM_LEVEL = 14.0
private val SMALL_SPEED_RANGE_IN_KMH = 0.0..10.0
private val MEDIUM_SPEED_RANGE_IN_KMH = 10.0..20.0
private val GREATER_SPEED_RANGE_IN_KMH = 20.0..40.0
private val BIG_SPEED_RANGE_IN_KMH = 40.0..70.0
private val HUGE_SPEED_RANGE_IN_KMH = 70.0..120.0

Screen shots presenting how the programmatic zooming functionality works:

Driving ZoomLevel