Map configuration
The Map Display module allows you to show the TomTom map in your Android application. To meet your use case requirements, you can apply initial settings during initialization using MapOptions or attributes . However, the map configuration can be also changed during runtime using the TomTomMap object.
Initializing the map in code
If the map is initialized in the Kotlin code, use the MapOptions object to provide initial configuration. Then when the map is displayed, all the settings will be already applied.
MapOptions properties are as follow:
Property | required/optional | Description |
required | Maps API key. | |
optional | | |
optional | | |
optional | Custom map style provided as | |
optional | Defines the | |
optional | Provides a custom |
Initializing the map in XML
If map initialization is performed in the layout file, the initial configuration can be set using attributes. The full list of attributes used to configure MapFragment is shown below. Each property can be mapped to the respective field in the MapOptions class. The same attributes can be applied to the MapView object.
<androidx.fragment.app.FragmentContainerView xmlns:tomtom="http://schemas.android.com/apk/res-auto" android:id="@+id/map_fragment" android:name="com.tomtom.sdk.map.display.ui.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tomtom:mapKey="YOUR_TOMTOM_API_KEY" tomtom:mapPaddingBottom="10" tomtom:mapPaddingLeft="10" tomtom:mapPaddingRight="10" tomtom:mapPaddingTop="10" tomtom:cameraTilt="45" tomtom:cameraRotation="20" tomtom:cameraPositionZoom="10" tomtom:cameraPositionLatitude="52.379189" tomtom:cameraPositionLongitude="4.899431" tomtom:cameraBoundsTopLeftLatitude="52.379189" tomtom:cameraBoundsTopLeftLongitude="4.899431" tomtom:cameraBoundsBottomRightLatitude="51.89275" tomtom:cameraBoundsBottomRightLongitude="5.67124" tomtom:styleUri="http://path.to.your.style" tomtom:styleDarkUri="http://path.to.your.dark.style" tomtom:styleLayerMappingUri="http://path.to.your.style.layer.mapping" tomtom:styleDarkLayerMappingUri="http://path.to.your.dark.style.layer.mapping" tomtom:styleMode="Dark" />Caching
Map caching can be configured using onlineCachePolicy , which you need to provide to the MapOptions object. The Map Display module caches:
- Map tiles.
- Styles.
- Sprites.
- Fonts.
Custom settings can be applied using the OnlineCachePolicy.Custom(Map<String, Duration>, Long) class. It requires two parameters:
-
sizeLimit- Maximum size of the cache in bytes.
By default, the cache size limit is set to 10MB.
Frame rate
By default, the map is rendered with a refresh rate of 60 FPS. You can change the frame rate, but note that the application will round the change to the nearest factor of 60 to ensure the new value will work with standard displays. This means that, for example, if you set the frame rate to 24 FPS, it will be rounded down to 20 FPS.
tomTomMap.setFrameRate(24)MapView lifecycle
If you are using MapView directly, you have to handle its lifecycle changes. They are used to control the underlying map renderer. The following methods must be bound to your activity or fragment lifecycle:
override fun onCreate( savedInstanceState: Bundle?, persistentState: PersistableBundle?,) { super.onCreate(savedInstanceState, persistentState) mapView.onCreate(savedInstanceState)}
override fun onStart() { super.onStart() mapView.onStart()}
override fun onResume() { super.onResume() mapView.onResume()}
override fun onPause() { super.onPause() mapView.onPause()}
override fun onStop() { super.onStop() mapView.onStop()}
override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) mapView.onSaveInstanceState(outState)}
override fun onDestroy() { super.onDestroy() mapView.onDestroy()}If the configuration changes because of screen rotation the same map instance is rendered. This means that any operations made on the map, such as camera position, map style and markers, are preserved.
Next steps
Since you have learned how to configure a map, here are recommendations for the next steps: