123456789101112131415161718192021222324252627 |
- import 'package:location/location.dart';
-
- Future getLocation() async {
- Location location = Location();
- bool _serviceEnabled;
- PermissionStatus _permissionGranted;
- LocationData _locationData;
-
- _serviceEnabled = await location.serviceEnabled();
- if (!_serviceEnabled) {
- _serviceEnabled = await location.requestService();
- if (!_serviceEnabled) {
- throw Exception("请开启系统定位功能");
- }
- }
-
- _permissionGranted = await location.hasPermission();
- if (_permissionGranted == PermissionStatus.denied) {
- _permissionGranted = await location.requestPermission();
- if (_permissionGranted != PermissionStatus.granted) {
- throw Exception("请设置允许当前程序定位功能");
- }
- }
-
- _locationData = await location.getLocation();
- return _locationData;
- }
|