startLocationUpdates method
Starts high precision location updates to keep track of the device's position. Returns true or an error, if permissions don't suffice. Automatically queries user permission if possible
Implementation
Future<bool> startLocationUpdates() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services disabled');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error('Location permissions permanently denied');
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
locationStream =
Geolocator.getPositionStream(locationSettings: LocationSettings(accuracy: LocationAccuracy.high))
.listen((Position position) {
//print(position.latitude.toString() + ', ' + position.longitude.toString());
currentLocation = position;
});
return true;
}