07-Module 7
07-Module 7
Mobile Programming
MODULE 7:
Sensors, Location, and Google Maps
Sensors
sensors_plus Flutter Package
• A Flutter plugin to access the accelerometer,
gyroscope, magnetometer and barometer
sensors.
sensors_plus Requirements
• Flutter >=3.19.0
• Dart >=3.3.0 <4.0.0
• iOS >=12.0
• MacOS >=10.14
• Android compileSDK 34
• Java 17
• Android Gradle Plugin >=8.3.0
• Gradle wrapper >=8.4
Add sensors_plus to your Flutter app
• Add sensors_plus as a dependency in your pubspec.yaml file.
<key>NSMotionUsageDescription</key>
<string>This app requires access to the barometer to provide altitude
information.</string>
• Caution
• Adding NSMotionUsageDescription is a requirement and not doing so will
crash your app when it attempts to access motion data.
Sensor Classes
• UserAccelerometerEvent describes the acceleration of the
device, in m/s2. If the device is still, or is moving along a
straight line at constant speed, the reported acceleration is
zero. If the device is moving e.g. towards north and its speed is
increasing, the reported acceleration is towards north; if it is
slowing down, the reported acceleration is towards south; if it
is turning right, the reported acceleration is towards east. The
data of this stream is obtained by filtering out the effect of
gravity from AccelerometerEvent.
Sensor
Classes
Sensor
Classes
Sensor
Classes
Add BroadcastStream to your Flutter app
magnetometerEvents(samplingPeriod:
SensorInterval.normalInterval).listen(
(MagnetometerEvent event) { Note
On Android it is not guaranteed that events from sensors will arrive
print(event); with specified sampling rate as it is noted in the official Android
}, documentation (see the description for the samplingPeriodUs
parameter). In reality delay varies depending on Android version,
onError: (error) { device hardware and vendor's OS customizations.
// Logic to handle error
// Needed for Android in case sensor is not available
},
cancelOnError: true,
);
Add Sensors to your Flutter app
Platform Restrictions and Considerations
The following lists the restrictions for the sensors on certain platforms due to
limitations of the platform.
• Currently it is not possible to set sensors sampling rate on web. Calls to event streams
at specied sampling periods will have the sampling period ignored.
Developers should consider alternative methods or inform users about the limitation when
their application runs on a web platform.
*Mozilla Developer Network (MDN), is a documentation repository and learning resource for web developers.
Add Sensors to your Flutter app
https://pub.dev/packages/sensors_plus/install
Location
15
Build location-aware apps
21
Add Location to your Flutter app
• The location plugin for Flutter handles getting
a location on Android and iOS. It also provides
callbacks when the location is changed.
dependencies:
location: ^5.0.0
Android
• To use location background mode on Android,
you have to use the
enableBackgroundMode({bool enable}) API
before accessing location in the background and
adding necessary permissions. You should place
Request the required permissions in your applications
Location • <uses-permission
android:name="android.permission.FOREGROUN
Permissions •
D_SERVICE" />
<uses-permission
android:name="android.permission.ACCESS_BAC
KGROUND_LOCATION"/>
// Deprecated, use
Location NSLocationAlwaysAndWhenInUseUsageDescription instead.
NSLocationAlwaysUsageDescription
Permissions // Use this very carefully. This key is required only if your iOS app
// uses APIs that access the user’s location information at all
times,
// even if the app isn't running.
NSLocationAlwaysAndWhenInUseUsageDescription
UIBackgroundModes
import 'package:location/location.dart’;
In order to request location, you should always check Location Service status
and Permission status manually
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;
location if (!_serviceEnabled) {
return;
}
}
28
Flutter Geolocator Plugin
Features
• Get the last known location;
• Get the current location of the device;
• Get continuous location updates;
• Check if location services are enabled on the device;
• Calculate the distance (in meters) between two
geocoordinates;
• Calculate the bearing between two geocoordinates;
Reference:
https://pub.dev/packages/geolocator
1
import 'package:geolocator/geolocator.dart';
2
LocationPermission permission;
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
4 }
32
Google Maps
33
Google Maps for Flutter
• Get an API key at https://cloud.google.com/maps-platform/.
• Enable Google Map SDK for each platform.
• Go to Google Developers Console.
• Choose the project that you want to enable Google Maps on.
• Select the navigation menu and then select "Google Maps".
• Select "APIs" under the Google Maps menu.
• To enable Google Maps for Android, select "Maps SDK for Android" in
the "Additional APIs" section, then select "ENABLE".
• To enable Google Maps for iOS, select "Maps SDK for iOS" in the
"Additional APIs" section, then select "ENABLE".
• To enable Google Maps for Web, enable the "Maps JavaScript API".
• Make sure the APIs you enabled are under the "Enabled APIs" section.
34
Adding
Google
•
Maps to a https://codelabs.de
velopers.google.co
m/codelabs/google
Flutter app -maps-in-flutter#0
35
End of Module 7