Add UI building blocks
This section presents various UI elements and related code snippets that are already available as part of the HERE SDK offering. From maneuver instructions and maneuver icon assets to visual route representation, discover how to integrate these elements into your application interface. More reusable UI building blocks are planned for the future.
Get maneuver instructions
Each Section of a Route object contains maneuver instructions a user may need to follow to reach the destination. For each turn, a Maneuver object contains an action and the location where the maneuver must be taken. The action may indicate actions like "depart" or directions such as "turn left".
let sections = route.sections
for section in sections {
logManeuverInstructions(section: section)
}And here is the code to access the maneuver instructions per section:
private func logManeuverInstructions(section: heresdk.Section) {
print("Log maneuver instructions per route section:")
let maneuverInstructions = section.maneuvers
for maneuverInstruction in maneuverInstructions {
let maneuverAction = maneuverInstruction.action
let maneuverLocation = maneuverInstruction.coordinates
let maneuverInfo = "\(maneuverInstruction.text)"
+ ", Action: \(maneuverAction)"
+ ", Location: \(maneuverLocation)"
print(maneuverInfo)
}
}This may be useful to easily build maneuver instructions lists describing the whole route in written form. For example, the ManeuverAction enum can be used to build your own unique routing experience.
Note (only applicable for Navigate)Note that the
Maneuverinstruction text (maneuverInstruction.text) is empty during navigation when it is taken fromNavigatororVisualNavigator. It only contains localized instructions when taken from aRouteinstance. TheManeuverActionenum is supposed to be used to show a visual indicator during navigation, and textual instructions fit more into a list to preview maneuvers before starting a trip.In opposition,
maneuverInstruction.roadTexts,maneuverInstruction.nextRoadTextsandmaneuverInstruction.exitSignTextsare meant to be shown as part of turn-by-turn maneuvers during navigation, so they are only non-empty when theManeuveris taken fromNavigatororVisualNavigator. If taken from aRouteinstance, these attributes are always empty.
Note (only applicable for Explore)The attributes
maneuverInstruction.roadTexts,maneuverInstruction.nextRoadTextsandmaneuverInstruction.exitSignTextsare only available for users of licenses such as Navigate as they are meant to be shown as part of turn-by-turn maneuvers during navigation. If taken from aRouteinstance, these attributes are always empty.
In the API Reference you can find an overview of the available maneuver actions.
The below table shows all ManeuverAction items with a preview description and an asset example. Note that the HERE SDK itself does not ship with maneuver icons. The assets are available as SVGs or solid PNGs in different densities as part of the open-source HERE Icon Library.
The available maneuver actions are sorted in the order as they appear in the API Reference:
Note that for now, the HERE assets for leftRoundaboutPass and rightRoundaboutPass are only available as SVGs - and some maneuver assets are only available in the sub-folder "wego-fallback-roundabout".
Get road shield icons
With iconProvider.createRoadShieldIcon(...) you can asynchronously create a Bitmap that depicts a road number such as "A7" or "US-101" - as it already appears on the map view.
The creation of road shield icons happens offline and does not require an internet connection. The data you need to create the icons is taken solely from the Route itself, but can be filled out also manually.
An example implementation of road shield display for maneuver previews is part of the "Rerouting" example. To get it, visit the example app repository on GitHub. Note that it requires the HERE SDK (Navigate), but the code for the IconProvider can be also used by other licenses, for example, to show road shield icons as part of a route preview.
Reusable maneuver panel and speed limit view (Navigate Only)
The following example apps show reusable UI building blocks that you can adapt to your own navigation experience.
- Maneuver panel view: See the Rerouting (iOS Swift) example app. It shows turn instructions, maneuver icons, and road shield icons during guidance.
- Speed limit view: See the TruckGuidance (iOS Swift) example app. It shows current truck and car speed limits as part of the guidance UI.
Use a maneuver panel view
To show the next maneuver during turn-by-turn navigation, create a custom view that can render a maneuver icon, a distance text, and a road name. Place that view on top of the map and update it from the VisualNavigator.
The full ManeuverModel and ManeuverView implementation is available in the Rerouting example app on GitHub.
The custom view is then placed on top of the map and bound to that model:
@StateObject private var maneuverModel = ManeuverModel()
var body: some View {
ZStack(alignment: .top) {
WrappedMapView(mapView: $mapView)
.edgesIgnoringSafeArea(.all)
VStack {
ManeuverView(model: maneuverModel)
.padding()
}
}
}The maneuver data itself comes from routeProgress.maneuverProgress. From there, resolve the next Maneuver, extract the action, remaining distance, and road name, and then update the bound model.
The following code shows the essential step of retrieving the next maneuver and updating the UI model:
private func parseManeuver(_ maneuverProgress: ManeuverProgress) -> String {
let nextManeuverIndex = maneuverProgress.maneuverIndex
guard let nextManeuver = visualNavigator.getManeuver(index: nextManeuverIndex) else {
return "Error: No next maneuver."
}
let action = nextManeuver.action
let roadName = getRoadName(maneuver: nextManeuver, route: visualNavigator.route)
let distanceText = convertDistance(meters: maneuverProgress.remainingDistanceInMeters)
onManeuverEvent(action: action, distanceText: distanceText, roadName: roadName)
return "Action: \(String(describing: action)) on \(roadName) in \(distanceText)"
}onManeuverEvent(...) is a small helper that updates the bound model:
private func onManeuverEvent(action: ManeuverAction, distanceText: String, roadName: String) {
maneuverModel.isManeuverPanelVisible = true
maneuverModel.distanceText = distanceText
maneuverModel.maneuverText = roadName
maneuverModel.maneuverIcon = maneuverIconProvider.getManeuverIconForAction(action)
}Use a speed limit view
To show the current legal speed limit, create a small custom view that displays one formatted value, for example, 50, NSL, or n/a. Place that view on top of the map and update it from speed limit events delivered by the navigator.
The full SpeedModel and SpeedView implementation is available in the TruckGuidance example app on GitHub.
The view is then added to the layout and bound to the model:
@StateObject private var truckSpeedLimitModel = SpeedModel()
var body: some View {
ZStack(alignment: .top) {
WrappedMapView(mapView: $mapView)
.edgesIgnoringSafeArea(.all)
VStack {
SpeedView(model: truckSpeedLimitModel)
}
}
}The speed limit update logic itself is straightforward: read speedLimit.effectiveSpeedLimitInMetersPerSecond(), convert the value to the unit you want to show, and update the bound model. When the returned value is 0, you can show NSL. When the value is unavailable, you can show n/a.
The following example shows the minimal integration logic:
func onSpeedLimitUpdated(_ speedLimit: heresdk.SpeedLimit) {
if let currentSpeedLimit = speedLimit.effectiveSpeedLimitInMetersPerSecond() {
if currentSpeedLimit == 0 {
truckSpeedLimitModel.speedText = "NSL"
} else {
let kmh = Int(metersPerSecondToKilometersPerHour(currentSpeedLimit))
truckSpeedLimitModel.speedText = "\(kmh)"
}
} else {
truckSpeedLimitModel.speedText = "n/a"
}
}If your application distinguishes between truck and car speed limits, you can use the same pattern as the TruckGuidance example app: use the VisualNavigator that follows the truck route for truck-specific limits, and a second Navigator in tracking mode to receive the corresponding car speed limits for the same location.
Show the route on the map
Below is a code snippet that shows how to show a route on the map by using a MapPolyline that is drawn between each coordinate of the route including the starting point and the destination:
let routeGeoPolyline = route.geometry
let widthInPixels = 20.0
let polylineColor = UIColor(red: 0, green: 0.56, blue: 0.54, alpha: 0.63)
do {
let routeMapPolyline = try MapPolyline(geometry: routeGeoPolyline,
representation: MapPolyline.SolidRepresentation(
lineWidth: MapMeasureDependentRenderSize(
sizeUnit: RenderSize.Unit.pixels,
size: widthInPixels),
color: polylineColor,
capShape: LineCap.round))
mapView.mapScene.addMapPolyline(routeMapPolyline)
} catch let error {
fatalError("Failed to render MapPolyline. Cause: \(error)")
}
mapView.mapScene.addMapPolyline(routeMapPolyline)The first screenshot below shows a route without additional waypoints - and therefore only one route section. Starting point and destination are indicated by green-circled map markers. Note that the code for drawing the circled objects is not shown here, but can be seen from the example's source code, if you are interested.
The second screenshot shows the same route as above, but with two additional stopover-waypoints, indicated by red-circled map markers. The route therefore, contains three route sections.
Additional stopover-waypoints split a route into separate sections and force the route to pass these points and to generate a maneuver instruction for each point.
Note that internally, rendering of the MapPolyline is optimized for very long routes. For example, on a higher zoom level, not every coordinate needs to be rendered, while for lower zoom levels, the entire route is not visible. The algorithm for this is not exposed, but the basic principle can be seen in the flexible-polyline open-source project from HERE.
Zoom to the route
For some use cases, it may be useful to zoom to the calculated route. The camera class provides a convenient method to adjust the viewport so that a route fits in:
let routeGeoBox = route?.boundingBox
camera.lookAt(area: routeGeoBox!,
orientation: GeoOrientationUpdate(bearing: nil, tilt: nil))Here we use the enclosing bounding box of the route object. This can be used to instantly update the camera: zoom level and target point of the camera will be changed, so that the given bounding rectangle fits exactly into the viewport. Additionally, we can specify an orientation to specify more camera parameters - here we keep the default values. Note that calling lookAt() will instantly change the view.
For most use cases, a better user experience is to zoom to the route with an animation. Below you can see an example that zooms to a GeoBox plus an additional padding of 50 pixels:
func animateToRoute(route: Route) {
// Untilt and unrotate the map.
let bearing: Double = 0
let tilt: Double = 0
// We want to show the route fitting in the map view with an additional padding of 50 pixels.
let origin:Point2D = Point2D(x: 50.0, y: 50.0)
let sizeInPixels:Size2D = Size2D(width: mapView.viewportSize.width - 100, height: mapView.viewportSize.height - 100)
let mapViewport:Rectangle2D = Rectangle2D(origin: origin, size: sizeInPixels)
// Animate to the route within a duration of 3 seconds.
let update:MapCameraUpdate = MapCameraUpdateFactory.lookAt(area: route.boundingBox, orientation: GeoOrientationUpdate(GeoOrientation(bearing: bearing, tilt: tilt)), viewRectangle: mapViewport)
let animation: MapCameraAnimation = MapCameraAnimationFactory.createAnimation(from: update, duration: TimeInterval(3), easing: Easing(EasingFunction.inCubic))
mapView.camera.startAnimation(animation)
}The CameraKeyframeTracks example app shows how this can look like.
Show traffic with routes
For information on how to visualize traffic conditions on routes, including rendering polylines adjacent to traffic flow and custom traffic overlays, see Visualize traffic on routes.
Updated 9 days ago





































