Map Controller
A map controller provides the interface between your map view, which is created using Mapbox, and the functionality available within the MapsGL SDK. Therefore, you will be working with a map controller instance most of the time when managing data sources and layers.
The following example demonstrates how to set up a map controller using Mapbox:
// Read in the Xweather account and access keys for the SDK that you set in strings.xml
var xweatherAccount = XweatherAccount(
getString(R.string.xweather_client_id),
getString(R.string.xweather_client_secret)
)
var mapView = binding.mapView
// Create the mapcontroller once the all the layout elements are loaded:
binding.mapView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
binding.mapView.viewTreeObserver.removeOnGlobalLayoutListener(this)
// Create a map controller that corresponds to the selected mapping library,
// passing in your `mapView` and `xweatherAccount` instances.
mapController = MapboxMapController(mapView, baseContext, xweatherAccount, this@MainActivity)
with(mapController) {
// Set the map to mercator mode:
mapboxMap.setProjection(projection(ProjectionName.MERCATOR))
// Listen for the loading of the map to finish:
mapboxMap.subscribeMapLoaded(mapLoadedCallback)
}
}
})
With a map controller created and the map loaded, you can start adding data to your map. To do so, you can quickly add our pre-configured weather layers (opens in a new tab) to your map without needing to create the data sources and layers directly.
// After the map has finished loading, add your weather layers
val mapLoadedCallback = MapLoadedCallback {
mapController.addWeatherLayer(WeatherService.Temperatures(mapController.service))
}
Review our guide on using weather layers for information on styling and customization.