location.dart 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'dart:async';
  2. import 'package:amap_flutter_location/amap_flutter_location.dart';
  3. import 'package:amap_flutter_location/amap_location_option.dart';
  4. import 'package:permission_handler/permission_handler.dart';
  5. import 'amap/const_config.dart';
  6. Future requireLocation() async {
  7. AMapLocationOption locationOption = AMapLocationOption();
  8. final AMapFlutterLocation location = AMapFlutterLocation();
  9. location.setLocationOption(locationOption);
  10. /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作
  11. ///
  12. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
  13. /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
  14. ///
  15. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
  16. ///
  17. /// [hasContains] 隐私声明中是否包含高德隐私政策说明
  18. ///
  19. /// [hasShow] 隐私权政策是否弹窗展示告知用户
  20. AMapFlutterLocation.updatePrivacyShow(true, true);
  21. /// 设置是否已经取得用户同意,如果未取得用户同意,高德定位SDK将不会工作
  22. ///
  23. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
  24. ///
  25. /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b>
  26. ///
  27. /// [hasAgree] 隐私权政策是否已经取得用户同意
  28. AMapFlutterLocation.updatePrivacyAgree(true);
  29. // 判断定位权限
  30. PermissionStatus _permissionGranted = await Permission.location.status;
  31. if (_permissionGranted == PermissionStatus.denied) {
  32. // 如果未允许, 尝试申请一次
  33. _permissionGranted = await Permission.location.request();
  34. if (_permissionGranted != PermissionStatus.granted) {
  35. throw Exception("请设置允许当前程序定位功能");
  36. }
  37. }
  38. AMapFlutterLocation.setApiKey(ConstConfig.amapApiKeys.androidKey!, ConstConfig.amapApiKeys.iosKey!);
  39. return location;
  40. }