• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Customize Route Line Style

This example shows:

  • Draw route line on the map using NavNextBillionMap

  • Customize the style of route line using RouteLineProperties when init the NavNextBillionMap in the _onStyleLoaded callback

  • RouteLineProperties

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
      /// The color of the shield icon for the route.
 	final Color routeShieldColor;

  	/// The color of the shield icon for alternative routes.
  	final Color alternativeRouteShieldColor;

  	/// The scale factor for the route line.
  	final double routeScale;

  	/// The scale factor for alternative route lines.
  	final double alternativeRouteScale;

  	/// The default color of the route line.
  	final Color routeDefaultColor;

  	/// The default color of alternative route lines.
  	final Color alternativeRouteDefaultColor;

  	/// The asset image name of the route origin marker.
  	final String originMarkerName;

  	/// The asset image name of the route destination marker.
  	final String destinationMarkerName;

  	/// The background color of the primary route's duration symbol.
  	final Color durationSymbolPrimaryBackgroundColor;

  	/// The background color of the alternative route's duration symbol.
  	final Color durationSymbolAlternativeBackgroundColor;

  	/// The text style for the primary route's duration symbol.
  	final TextStyle durationSymbolPrimaryTextStyle;

  	/// The text style for alternative route's duration symbols.
  	final TextStyle durationSymbolAlternativeTextStyle;
Android snapshotiOS snapshot

For all code examples, refer to Flutter Navigation Code Example

RouteLineStyle 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
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 RouteLineStyle extends StatefulWidget {
  static const String title = "Customize Route Line Style";

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

class RouteLineStyleState extends State<RouteLineStyle> {
  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(RouteLineStyle.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,
      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 = false;
    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 draw a route line on nbMap, customize the route line style, and navigate along the route. The app uses the nb_maps_flutter and nb_navigation_flutter packages for map rendering, route drawing, and navigation-related functionality.

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. 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.