Location Component

This example shows

  • Configuration Location permissions for Android and iOS
  • Enable Current Location Tracking with setting following params in NBMap
    • trackCameraPosition: true
    • myLocationEnabled: true
    • myLocationTrackingMode: MyLocationTrackingMode.Tracking
  • Observe User Location Updating
    • add the callback onUserLocationUpdated(UserLocation location)
documentation imagedocumentation image
Android snapshotiOS snapshot

For all code examples, refer to Flutter Maps SDK Code Examples

Android: Add the following permissions to the manifest:

1
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
3
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

iOS: Add the following to the Runner/Info.plist to explain why you need access to the location data:

1
<key>NSLocationWhenInUseUsageDescription</key>
2
<string>[Your explanation here]</string>

TrackCurrentLocation view source

1
import 'package:flutter/foundation.dart';
2
import 'package:flutter/material.dart';
3
import 'package:nb_navigation_flutter/nb_navigation_flutter.dart';
4
5
class TrackCurrentLocation extends StatefulWidget {
6
static const String title = "Track Current Location";
7
8
const TrackCurrentLocation({super.key});
9
10
@override
11
TrackCurrentLocationState createState() => TrackCurrentLocationState();
12
}
13
14
class TrackCurrentLocationState extends State<TrackCurrentLocation> {
15
NextbillionMapController? controller;
16
17
String locationTrackImage = "assets/location_on.png";
18
19
void _onMapCreated(NextbillionMapController controller) {
20
this.controller = controller;
21
}
22
23
_onStyleLoadedCallback() async {
24
Future.delayed(const Duration(milliseconds: 80), () {
25
controller?.updateMyLocationTrackingMode(MyLocationTrackingMode.Tracking);
26
});
27
}
28
29
_onUserLocationUpdate(UserLocation location) {
30
if (kDebugMode) {
31
print('${location.position.longitude}, ${location.position.latitude}');
32
}
33
}
34
35
_onCameraTrackingChanged() {
36
setState(() {
37
locationTrackImage = 'assets/location_off.png';
38
});
39
}
40
41
@override
42
Widget build(BuildContext context) {
43
return Scaffold(
44
appBar: AppBar(
45
title: const Text(TrackCurrentLocation.title),
46
),
47
body: Stack(
48
children: [
49
NBMap(
50
onMapCreated: _onMapCreated,
51
onStyleLoadedCallback: _onStyleLoadedCallback,
52
initialCameraPosition: const CameraPosition(
53
target: LatLng(0, 0),
54
zoom: 14.0,
55
),
56
trackCameraPosition: true,
57
myLocationEnabled: true,
58
myLocationTrackingMode: MyLocationTrackingMode.Tracking,
59
onUserLocationUpdated: _onUserLocationUpdate,
60
onCameraTrackingDismissed: _onCameraTrackingChanged,
61
),
62
Column(
63
mainAxisAlignment: MainAxisAlignment.end,
64
crossAxisAlignment: CrossAxisAlignment.start,
65
children: [
66
Padding(
67
padding: const EdgeInsets.only(left: 10.0, bottom: 100),
68
child: GestureDetector(
69
child: Image(
70
image: AssetImage(locationTrackImage),
71
width: 28,
72
height: 28,
73
),
74
onTap: () {
75
controller?.updateMyLocationTrackingMode(
76
MyLocationTrackingMode.Tracking);
77
setState(() {
78
locationTrackImage = 'assets/location_on.png';
79
});
80
}),
81
),
82
],
83
)
84
],
85
),
86
);
87
}
88
89
@override
90
void initState() {
91
super.initState();
92
}
93
94
@override
95
void dispose() {
96
super.dispose();
97
}
98
}
99

Code summary

This Flutter code provides an example of how to use the NBMap widget from the nb_maps_flutter package to display a map and enable current location tracking. The code also observes user location updates and changes the tracking mode when the camera tracking is dismissed.

1. Display MapView Widget:

  • The NBMap widget is used to display the map on the screen. It is added to the Stack widget to overlay other UI elements on top of the map.

2. Enable Current Location Tracking: The following parameters are set in the NBMap widget to enable current location tracking:

  • myLocationEnabled: true enables the display of the user's location on the map.
  • myLocationTrackingMode: MyLocationTrackingMode.Tracking sets the tracking mode for the user's location, which follows the user's movement on the map.
  • onUserLocationUpdated: _onUserLocationUpdate is a callback function that observes updates to the user's location.
  1. Observe User Location Updating:
  • The _onUserLocationUpdate function is called when the user's location is updated. In this code, it prints the updated longitude and latitude to the console.

The code also has a gesture detector with an image button to toggle the location tracking mode. When the image button is tapped, the tracking mode is set to MyLocationTrackingMode.Tracking, and the image changes accordingly.

To use this widget in your Flutter app, make sure you have installed the nb_maps_flutter package and added the necessary dependencies to your pubspec.yaml file.

Overall, this code demonstrates how to display a map, enable current location tracking, and observe user location updates using the NBMap widget and the NextbillionMap SDK in a Flutter app.

© 2024 NextBillion.ai all rights reserved.