location_utils.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:amap_flutter_location/amap_flutter_location.dart';
  2. import 'package:amap_flutter_location/amap_location_option.dart';
  3. import 'package:permission_handler/permission_handler.dart';
  4. class LocationUtils {
  5. LocationUtils._();
  6. ///设置Android和iOS的apikey,建议在weigdet初始化时设置<br>
  7. ///apiKey的申请请参考高德开放平台官网<br>
  8. ///Android端: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key<br>
  9. ///iOS端: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key<br>
  10. ///[androidKey] Android平台的key<br>
  11. ///[iosKey] ios平台的key<br>
  12. static void setApiKey(String androidKey, String iosKey) {
  13. AMapFlutterLocation.setApiKey(androidKey, iosKey);
  14. }
  15. /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作<br>
  16. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy<br>
  17. /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b><br>
  18. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
  19. /// [hasContains] 隐私声明中是否包含高德隐私政策说明<br>
  20. /// [hasShow] 隐私权政策是否弹窗展示告知用户<br>
  21. static void updatePrivacyShow(bool hasContains, bool hasShow) {
  22. AMapFlutterLocation.updatePrivacyShow(hasContains, hasShow);
  23. }
  24. /// 设置是否已经取得用户同意,如果未取得用户同意,高德定位SDK将不会工作<br>
  25. /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy<br>
  26. /// <b>必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意</b><br>
  27. /// [hasAgree] 隐私权政策是否已经取得用户同意<br>
  28. static void updatePrivacyAgree(bool hasAgree) {
  29. AMapFlutterLocation.updatePrivacyAgree(hasAgree);
  30. }
  31. static AMapFlutterLocation? _location;
  32. static AMapFlutterLocation get location => _location ??= AMapFlutterLocation();
  33. /// 设置定位参数
  34. static AMapFlutterLocation setLocationOption(AMapLocationOption option) {
  35. location.setLocationOption(option);
  36. return location;
  37. }
  38. ///开始定位
  39. static AMapFlutterLocation startLocation() {
  40. location.startLocation();
  41. return location;
  42. }
  43. ///停止定位
  44. static AMapFlutterLocation stopLocation() {
  45. location.stopLocation();
  46. return location;
  47. }
  48. ///销毁定位
  49. static void destroy() {
  50. location.destroy();
  51. _location = null;
  52. }
  53. ///定位结果回调
  54. ///
  55. ///定位结果以map的形式透出,其中包含的key已经含义如下:
  56. ///
  57. /// `callbackTime`:回调时间,格式为"yyyy-MM-dd HH:mm:ss"
  58. ///
  59. /// `locationTime`:定位时间, 格式为"yyyy-MM-dd HH:mm:ss"
  60. ///
  61. /// `locationType`: 定位类型, 具体类型可以参考https://lbs.amap.com/api/android-location-sdk/guide/utilities/location-type
  62. ///
  63. /// `latitude`:纬度
  64. ///
  65. /// `longitude`:精度
  66. ///
  67. /// `accuracy`:精确度
  68. ///
  69. /// `altitude`:海拔, android上只有locationType==1时才会有值
  70. ///
  71. /// `bearing`: 角度,android上只有locationType==1时才会有值
  72. ///
  73. /// `speed`:速度, android上只有locationType==1时才会有值
  74. ///
  75. /// `country`: 国家,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  76. ///
  77. /// `province`: 省,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  78. ///
  79. /// `city`: 城市,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  80. ///
  81. /// `district`: 城镇(区),android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  82. ///
  83. /// `street`: 街道,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  84. ///
  85. /// `streetNumber`: 门牌号,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  86. ///
  87. /// `cityCode`: 城市编码,android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  88. ///
  89. /// `adCode`: 区域编码, android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  90. ///
  91. /// `address`: 地址信息, android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  92. ///
  93. /// `description`: 位置语义, android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  94. ///
  95. /// `errorCode`: 错误码,当定位失败时才会返回对应的错误码, 具体错误请参考:https://lbs.amap.com/api/android-location-sdk/guide/utilities/errorcode
  96. ///
  97. /// `errorInfo`: 错误信息, 当定位失败时才会返回
  98. static Stream<Map<String, Object>> onLocationChanged() {
  99. return location.onLocationChanged();
  100. }
  101. /// 申请定位权限
  102. /// 授予定位权限返回true, 否则返回false
  103. static Future<bool> requestLocationPermission() async {
  104. //获取当前的权限
  105. var status = await Permission.location.status;
  106. if (status == PermissionStatus.granted) {
  107. //已经授权
  108. return true;
  109. } else {
  110. //未授权则发起一次申请
  111. status = await Permission.location.request();
  112. if (status == PermissionStatus.granted) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. }
  119. }