• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Customize Navigation Style

This Example Shows

  • Draw route lines on the map view
  • Customize Navigation view style
    • iOS: Import nb_navigation_flutter in the AppDelegate of your ios project, customize the navigation view appearance using customDayStyle and customNightStyle
    • Android: Add Custom style named CustomNavigationViewLight and CustomNavigationViewDark in your android project styles.xml with parent = "NavigationViewLight" or parent = "NavigationViewDark"
  • Use customize navigation style by setting useCustomNavigationStyle to TRUE in NavigationLauncherConfig when startNavigation
Android snapshotiOS snapshot

For all code examples, refer to Flutter Navigation Code Example

styles.xml in android project view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<style name="CustomNavigationViewLight" parent="NavigationViewLight">
        <!--customize your navigation style-->
        <item name="navViewRouteStyle">@style/CustomRouteStyle</item>
        <item name="navViewBannerBackground">#FFC84E0D</item>
        <item name="navViewBannerPrimaryText">#FF9E10FF</item>
        <item name="navViewBannerSecondaryText">#FFFA8C1C</item>
        <item name="navViewBannerManeuverPrimary">#FF4E2EFF</item>
        <item name="navViewPrimaryText">#FFFF0115</item>
        <item name="navViewSecondaryText">#FF1A6620</item>
        <item name="navigationViewRouteOverviewDrawable">@drawable/ic_baseline_navigation_24</item>
        <item name="navViewRouteOverviewBackground">#FF41FF00</item>
        <item name="navViewListBackground">#FFFFC0A8</item>
        <item name="navViewListPrimary">#FF6E7ACA</item>
        <item name="navViewListSecondary">#FF003CE0</item>
        <item name="navViewListManeuverPrimary">#FFA90403</item>
        <item name="navViewListManeuverSecondary">#FFA90403</item>
        <item name="navigationViewReCentreInfoColor">#FFA90403</item>
        <item name="navigationViewReCentreBackgroundDrawable">@drawable/recenter_btn_background</item>
        <item name="navViewSoundColor">#FFA90403</item>
        <item name="navViewSoundBackgroundColor">#FF6E7ACA</item>
        <item name="navViewPauseColor">#FFFFC0A8</item>
        <item name="navViewPauseBackgroundColor">#FFFFFFFF</item>
        <item name="navViewRestartColor">#FFFFC0A8</item>
        <item name="navViewRestartBackgroundColor">#FFFFFFFF</item>
        <item name="navigationCurrentSpeedBackgroundColor">#FF6E7ACA</item>
        <item name="navigationCurrentSpeedTextColor">#FF000000</item>
        <item name="navigationCurrentSpeedUnitTextColor">#FF9E10FF</item>
    </style>


    <style name="CustomRouteStyle" parent="NavigationMapRoute">
        <item name="routeColor">#FFE97F2F</item>
        <item name="routeScale">1.0</item>
        <item name="alternativeRouteScale">1.0</item>
        <item name="routeShieldColor">#FF54E910</item>
        <item name="routeFloatDurationBackgroundPrimary">#FFE97F2F</item>
        <item name="maneuverArrowColor">#FFFF0106</item>
        <item name="maneuverArrowBorderColor">#FF70C35D</item>
        <item name="routeWayNameBackGroundColor">#FFE97F2F</item>
        <item name="routeWayNameTextColor">#FFFFFFFF</item>
        <item name="navigationDissolvedRouteColor">#FFFFC0A8</item>
        <item name="navigationDissolvedAnimatedRouteColor">#FF54E910</item>
    </style>


 <style name="CustomNavigationViewDark" parent="NavigationViewDark">
        <!--customize your navigation style-->

    </style>

AppDelegate.swift in iOS project view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import UIKit
import Flutter
import nb_navigation_flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        customStyle()
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    func customStyle() {
        customDayStyle.arrivalTimeLabelFont = UIFont.systemFont(ofSize: 28, weight: .medium).adjustedFont
        customDayStyle.trafficUnknownColor = UIColor.red
        customDayStyle.nextBannerViewBackgroundColor = #colorLiteral(red: 0.7843137255, green: 0.3058823529, blue: 0.05098039216, alpha: 1)
        customDayStyle.maneuverNextBannerPrimaryColor = #colorLiteral(red: 0.6196078431, green: 0.06274509804, blue: 1, alpha: 1)
        customNightStyle.trafficUnknownColor = UIColor.green
    }
}

CustomNavigationStyle view source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nb_navigation_flutter/nb_navigation_flutter.dart';
import 'package:nb_maps_flutter/nb_maps_flutter.dart';

class CustomNavigationStyle extends StatefulWidget {
  static const String title = "Customize Navigation Style";

  @override
  CustomNavigationStyleState createState() => CustomNavigationStyleState();
}

class CustomNavigationStyleState extends State<CustomNavigationStyle> {
  NextbillionMapController? controller;
  List<DirectionsRoute> routes = [];
  late NavNextBillionMap navNextBillionMap;

  LatLng origin = LatLng(1.312533169133601, 103.75986708439264);
  LatLng dest = LatLng(1.310473772283314, 103.77982271935586);

  void _onMapCreated(NextbillionMapController controller) {
    this.controller = controller;
  }

  void _onStyleLoaded() {
    if (controller != null) {
      var routeLineStyle = const RouteLineProperties(
          routeDefaultColor: Color(0xFFE97F2F),
          routeScale: 1.0,
          alternativeRouteScale: 1.0,
          routeShieldColor: Color(0xFF54E910),
          durationSymbolPrimaryBackgroundColor: Color(0xFFE97F2F)
      );
      navNextBillionMap = NavNextBillionMap(controller!, routeLineProperties: routeLineStyle);
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(CustomNavigationStyle.title),
      ),
      body: Stack(
        children: [
          NBMap(
            onMapCreated: _onMapCreated,
            initialCameraPosition: CameraPosition(
              target: LatLng(origin.latitude, 103.76986708439264),
              zoom: 13.0,
            ),
            onStyleLoadedCallback: _onStyleLoaded,
          ),
          _buttonWidget(),
        ],
      ),
    );
  }

  void _fetchRoute() async {
    RouteRequestParams requestParams = RouteRequestParams(
      origin: origin,
      destination: dest,
      // waypoints: [Coordinate(latitude: wayP2.latitude, longitude: wayP2.longitude)],
      // overview: ValidOverview.simplified,
      // avoid: [SupportedAvoid.toll, SupportedAvoid.ferry],
      // option: SupportedOption.flexible,
      // truckSize: [200, 200, 600],
      // truckWeight: 100,
      unit: SupportedUnits.imperial,
      mode: ValidModes.car,
      geometryType: SupportedGeometry.polyline,
    );

    await NBNavigation.fetchRoute(requestParams, (routes, error) async {
      if (routes.isNotEmpty) {
        setState(() {
          this.routes = routes;
        });
        drawRoutes(routes);
      } else if (error != null) {
        print("====error====${error}");
      }
    });
  }

  void _startNavigation() {
    if (routes.isEmpty) return;
    NavigationLauncherConfig config = NavigationLauncherConfig(route: routes.first, routes: routes);
    config.locationLayerRenderMode = LocationLayerRenderMode.GPS;
    config.enableDissolvedRouteLine = false;
    config.shouldSimulateRoute = true;
    config.themeMode = NavigationThemeMode.system;
    config.useCustomNavigationStyle = true;
    NBNavigation.startNavigation(config);
  }

  Future<void> drawRoutes(List<DirectionsRoute> routes) async {
    navNextBillionMap.clearRoute();
    await navNextBillionMap.drawRoute(routes);
  }

  @override
  void dispose() {
    super.dispose();
  }

  _buttonWidget() {
    return Positioned(
      bottom: 60,
      child: Padding(
        padding: const EdgeInsets.only(left: 8, top: 18.0),
        child: Row(
          children: [
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.blueAccent),
              ),
              onPressed: () {
                _fetchRoute();
              },
              child: const Text("Fetch Route"),
            ),
            const Padding(padding: EdgeInsets.only(left: 8)),
            ElevatedButton(
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(routes.isEmpty ? Colors.grey : Colors.blueAccent),
                  enableFeedback: routes.isNotEmpty),
              onPressed: () {
                _startNavigation();
              },
              child: const Text("Start Navigation"),
            ),
          ],
        ),
      ),
    );
  }
}

Code summary

The above code snippet demonstrates how to customize the navigation style for turn-by-turn navigation. The app uses the nb_maps_flutter and nb_navigation_flutter packages for map rendering, route drawing, and navigation-related functionality.

Customize Navigation Style:

  • iOS: Import nb_navigation_flutter in the AppDelegate of your iOS project, customize the navigation view appearance using customDayStyle and customNightStyle
  • Android: Add Custom style named CustomNavigationViewLight and CustomNavigationViewDark in your android project styles.xml with parent = "NavigationViewLight" or parent = "NavigationViewDark"
  • Use customize navigation style by setting useCustomNavigationStyle to TRUE in NavigationLauncherConfig when startNavigation

Draw Route Line:

  • The NBMap widget from the nb_maps_flutter package is used to display the map.
  • When the "Fetch Route" button is pressed, the _fetchRoute function is called. It sends a route request to the navigation service (NBNavigation.fetchRoute) with the specified origin and destination coordinates. The response contains a list of DirectionsRoute objects representing different possible routes from the origin to the destination.
  • The drawRoutes function is used to draw the fetched routes on the map using the NavNextBillionMap controller.

Customize Route Line Style:

  • The _onStyleLoaded function is called when the map style is loaded. It initializes a RouteLineProperties object to customize the appearance of the route line.
  • The NavNextBillionMap controller is created with the specified routeLineStyle object, which customizes attributes like the route color, route scale, alternative route scale, route shield color, and duration symbol background color.

Supported Route Line Style Attributes:

The RouteLineProperties class provides the following supported route line style attributes:

  • routeDefaultColor: The default color of the main route line.

  • routeScale: The scaling factor applied to the width of the main route line.

  • alternativeRouteScale: The scaling factor applied to the width of alternative route lines.

  • routeShieldColor: The color of the route shield, which is displayed on the map for certain types of routes.

  • durationSymbolPrimaryBackgroundColor: The background color of the duration symbol displayed along the route line.

  • alternativeRouteDefaultColor: The default color of alternative route lines.

  • originMarkerName: The asset image name of the route origin marker.

  • destinationMarkerName: The asset image name of the route destination marker.

  • durationSymbolPrimaryBackgroundColor: The background color of the primary route's duration symbol.

  • durationSymbolAlternativeBackgroundColor: The background color of the alternative route's duration symbol.

  • durationSymbolPrimaryTextStyle: The text style for the primary route's duration symbol.

  • durationSymbolAlternativeTextStyle: The text style for alternative route's duration symbols.

Start Navigation:

  • The "Start Navigation" button is used to initiate turn-by-turn navigation using the selected route.

  • When the button is pressed, the _startNavigation function is called. It launches the navigation using NBNavigation.startNavigation with the first route from the list.

Please note that some parts of the code (e.g., fetchRoute and startNavigation) rely on external services or APIs provided by the nb_navigation_flutter package, which are not fully visible from the code snippet. These services handle the actual route fetching and navigation functionality. Also, ensure that you have the required permissions and API keys set up to use the mapping and navigation services properly.