Location context
While navigating, you can retrieve detailed information about the current location on the road using LocationContext .
The SDK provides location context data such as:
- The current speed
- The speed limit
- The properties of the road (for example, the number of lanes, the functional road class and the driving side)
- The address (for example, the name of the city, the name of the street and the number on the street).
Refer to the LocationContext API reference for more details.
This guide explains to you how to use LocationContext to retrieve the name of the city and the name of the street at the current location.
Starting navigation
Before retrieving location context information, you need to start navigation with a route as described in the Starting navigation guide .
But, instead of adding a listener for route progress updates, make sure you add a NavigationLocationContextObserver to listen to location context updates.
func startNavigation() throws { let tomTomNavigation = try OnlineTomTomNavigationFactory.create(configuration: navigationConfiguration) tomTomNavigation.addLocationContextObserver(self)
let routePlan = RoutePlan(route: route, routePlanningOptions: routePlanningOptions) let navigationOptions = NavigationOptions(activeRoutePlan: routePlan) try tomTomNavigation.start(navigationOptions: navigationOptions)}Retrieving location context information
With navigation started, you can listen to location context updates and retrieve the name of the city and the name of the street at the current location.
func didDetectLocationContext(locationContext: LocationContext) { let speed: Measurement<UnitSpeed> = locationContext.speed let speedLimit: TomTomSDKCommon.SpeedLimit? = locationContext.speedLimit
let countryCode: String? = locationContext.address?.countryCodeISO3 let city: String? = locationContext.address?.municipality let streetName: String? = locationContext.address?.streetName
let functionalRoadClass: Int? = locationContext.road?.functionalRoadClass let isRoadUnderpass: Bool? = locationContext.road?.isUnderpass let isRoadTunnel: Bool? = locationContext.road?.isTunnel let tunnelName: String? = locationContext.road?.tunnelName let isRoadBridge: Bool? = locationContext.road?.isBridge let bridgeName: String? = locationContext.road?.bridgeName}Next steps
Now that you know how to retrieve location context information, here are the recommendations on what to explore next: