geolocator插件地址
geolocator 插件是一个Flutter定位插件,支持 Android,iOS,macOS,Web,Windows中定位。
使用前需要针对不同平台配置权限,按照官网步骤完成即可。
示例:
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
| import 'package:geolocator/geolocator.dart';
Future<Position> _determinePosition() async { bool serviceEnabled; LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { return Future.error('位置服务已禁用。'); }
permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { return Future.error('位置权限被拒绝'); } }
if (permission == LocationPermission.deniedForever) { return Future.error( '位置权限被永久拒绝,我们不能请求权限。'); }
return await Geolocator.getCurrentPosition(); }
|