This example demonstrates switching between different map types in OpenMapView, showing various OpenStreetMap-based tile sources with automatic attribution updates.
- Five map types: Normal, Terrain, Humanitarian, Cycle, None
- Dynamic map type switching with buttons
- Automatic attribution text updates
- Tile cache clearing on map type change
- Material3 UI with button controls
- Error handling for invalid map types
- Open the OpenMapView project in Android Studio
- Select
examples.Example11MapTypesfrom the run configuration dropdown - Click Run (green play button)
- Deploy to your device or emulator
# From project root - build, install, and launch
./gradlew :examples:Example11MapTypes:installDebug
# Launch the app
adb shell am start -n de.afarber.openmapview.example11maptypes/.MainActivityval mapView = OpenMapView(context)
// Set map type
mapView.setMapType(MapType.TERRAIN)
// Get current map type
val currentType = mapView.getMapType()try {
mapView.setMapType(99)
} catch (e: IllegalArgumentException) {
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
}- MapType.NORMAL: Standard OpenStreetMap road map (default)
- MapType.TERRAIN: OpenTopoMap with elevation contours
- MapType.HUMANITARIAN: HOT OSM emphasizing humanitarian features
- MapType.CYCLE: CyclOSM showing cycling infrastructure
- MapType.NONE: No base map tiles displayed
- setMapType(): Change the map's tile source
- getMapType(): Query current map type
- Launch the app - Default Normal map type loads
- Click Terrain - Switches to OpenTopoMap with contour lines
- Click Humanitarian - Switches to red/orange humanitarian style
- Click Cycle - Switches to CyclOSM with bike lanes
- Click None - Removes all base tiles (overlays still visible)
- Click Normal - Returns to standard OSM tiles
- Check attribution - Bottom-right text updates for each source
- Pan and zoom - New tiles load from current source
| Type | Constant | Tile Source | Best For |
|---|---|---|---|
| Normal | MapType.NORMAL |
OpenStreetMap Standard | General purpose street maps |
| Terrain | MapType.TERRAIN |
OpenTopoMap | Hiking, elevation, topography |
| Humanitarian | MapType.HUMANITARIAN |
Humanitarian OSM | Emergency response, infrastructure |
| Cycle | MapType.CYCLE |
CyclOSM | Cycling routes, bike infrastructure |
| None | MapType.NONE |
No tiles | Custom overlays only, performance testing |
- Tile source URL template is updated
- Memory tile cache is cleared
- Disk tile cache is cleared
- Attribution text is updated
- Map view is invalidated and redraws
- Normal: Map data from OpenStreetMap
- Terrain: Map data from OpenStreetMap | Map style: OpenTopoMap (CC-BY-SA)
- Humanitarian: Humanitarian OpenStreetMap Team
- Cycle: CyclOSM | Map data from OpenStreetMap
- Cache clearing may cause momentary blank tiles
- New tiles download asynchronously
- Each map type has independent cache
- Some tile servers may have rate limits
val mapTypes = listOf(
MapType.NORMAL,
MapType.TERRAIN,
MapType.HUMANITARIAN,
MapType.CYCLE
)
// Cycle through map types
var currentIndex = 0
button.setOnClickListener {
currentIndex = (currentIndex + 1) % mapTypes.size
mapView.setMapType(mapTypes[currentIndex])
}when (mapView.getMapType()) {
MapType.NORMAL -> showNormalControls()
MapType.TERRAIN -> showElevationLegend()
MapType.CYCLE -> showCyclingStats()
else -> hideSpecialControls()
}Default Center: Bochum, Germany (51.4661°N, 7.2491°E) at zoom 13.0
Test different map types at different locations:
- Terrain: Best viewed in mountainous areas (Alps, Black Forest)
- Humanitarian: Shows infrastructure in any populated area
- Cycle: Best in cities with cycling infrastructure
