张延森 3 年 前
コミット
30c7a0193e

+ 3
- 3
android/app/build.gradle ファイルの表示

@@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
26 26
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27 27
 
28 28
 android {
29
-    compileSdkVersion flutter.compileSdkVersion
29
+    compileSdkVersion 31
30 30
 
31 31
     compileOptions {
32 32
         sourceCompatibility JavaVersion.VERSION_1_8
@@ -36,8 +36,8 @@ android {
36 36
     defaultConfig {
37 37
         // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
38 38
         applicationId "com.njyunzhi.farmer_client"
39
-        minSdkVersion flutter.minSdkVersion
40
-        targetSdkVersion flutter.targetSdkVersion
39
+        minSdkVersion 21
40
+        targetSdkVersion 28
41 41
         versionCode flutterVersionCode.toInteger()
42 42
         versionName flutterVersionName
43 43
     }

+ 3
- 0
android/app/src/main/AndroidManifest.xml ファイルの表示

@@ -59,5 +59,8 @@
59 59
            android:value="b481b4187e24e625fa0056fd0530e663"/>
60 60
        <service android:name="com.amap.api.location.APSService"
61 61
            tools:ignore="MissingClass" />
62
+<!--       <meta-data-->
63
+<!--           android:name="com.baidu.lbsapi.API_KEY"-->
64
+<!--           android:value="p8WUiN5XtPrBYagGfld4VZFCnM4lo3Dq" />-->
62 65
     </application>
63 66
 </manifest>

バイナリ
android/app/src/main/res/mipmap-hdpi/ic_launcher.png ファイルの表示


バイナリ
android/app/src/main/res/mipmap-mdpi/ic_launcher.png ファイルの表示


バイナリ
android/app/src/main/res/mipmap-xhdpi/ic_launcher.png ファイルの表示


バイナリ
android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png ファイルの表示


バイナリ
android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png ファイルの表示


+ 4
- 0
lib/main.dart ファイルの表示

@@ -1,4 +1,5 @@
1 1
 import 'package:farmer_client/theme.dart';
2
+import 'package:farmer_client/utils/location.dart';
2 3
 import 'package:flutter/material.dart';
3 4
 import 'package:get/get.dart';
4 5
 import 'package:get_storage/get_storage.dart';
@@ -12,6 +13,9 @@ import 'routes/index.dart';
12 13
 void main() async {
13 14
   await GetStorage.init();
14 15
 
16
+  // // 高德地图隐私生命
17
+  // showPrivacy();
18
+
15 19
   runApp(const MyApp());
16 20
 }
17 21
 

+ 25
- 20
lib/models/app.dart ファイルの表示

@@ -4,8 +4,11 @@ import 'dart:io';
4 4
 
5 5
 import 'package:amap_flutter_location/amap_flutter_location.dart';
6 6
 import 'package:farmer_client/models/entities/person.dart';
7
+import 'package:farmer_client/utils/timer.dart';
7 8
 import 'package:farmer_client/widgets/Modal.dart';
9
+import 'package:flutter/material.dart';
8 10
 import 'package:get/get.dart';
11
+import '../utils/amap/const_config.dart';
9 12
 import '../utils/location.dart';
10 13
 
11 14
 class AppController extends GetxController {
@@ -17,7 +20,6 @@ class AppController extends GetxController {
17 20
   final testInt = 1.obs;
18 21
 
19 22
   AMapFlutterLocation? _location;
20
-  StreamSubscription<Map<String, Object>>? _locationListener;
21 23
 
22 24
   get locationStr {
23 25
     if (null == location.value) return null;
@@ -28,27 +30,34 @@ class AppController extends GetxController {
28 30
     return longitude.toString() + "," + latitude.toString();
29 31
   }
30 32
 
33
+  void onLocationChange (Map<String, Object> result) {
34
+    location(result);
35
+  }
36
+
31 37
   @override
32 38
   void onInit() {
33 39
     super.onInit();
34 40
 
35
-    // 尝试获取 location
36
-    requireLocation().then((loc) async {
37
-      _location = loc;
41
+    setTimeout(() {
42
+
43
+      // 尝试获取 location
44
+      requireLocation(onLocationChange).then((loc) {
45
+        _location = loc;
46
+      }).catchError((e) {
47
+        print(e);
38 48
 
39
-      // 监听位置变化
40
-      _locationListener = loc.onLocationChanged().listen((Map<String, Object> result) {
41
-        location(result);
49
+        showAlert(
50
+          title: '获取定位失败',
51
+          message: e.message,
52
+          onConfirm: () => exit(1),
53
+        );
42 54
       });
43
-      loc.startLocation();
44
-    }).catchError((e) {
45
-      print(e);
46
-
47
-      showAlert(
48
-        title: '获取定位失败',
49
-        message: e.message,
50
-        onConfirm: () => exit(1),
51
-      );
55
+    }, 2000);
56
+    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
57
+      AMapFlutterLocation.setApiKey(ConstConfig.amapApiKeys.androidKey!, ConstConfig.amapApiKeys.iosKey!);
58
+      // showPrivacy();
59
+      AMapFlutterLocation.updatePrivacyShow(true, true);
60
+      AMapFlutterLocation.updatePrivacyAgree(true);
52 61
     });
53 62
 
54 63
     // // 尝试获取一次人员信息
@@ -61,10 +70,6 @@ class AppController extends GetxController {
61 70
 
62 71
   @override
63 72
   void onClose() {
64
-    if (null != _locationListener) {
65
-      _locationListener?.cancel();
66
-    }
67
-
68 73
     if (null != _location) {
69 74
       _location?.destroy();
70 75
     }

+ 91
- 6
lib/utils/location.dart ファイルの表示

@@ -6,11 +6,9 @@ import 'package:permission_handler/permission_handler.dart';
6 6
 
7 7
 import 'amap/const_config.dart';
8 8
 
9
-Future requireLocation() async {
10
-  AMapLocationOption locationOption = AMapLocationOption();
11
-  final AMapFlutterLocation location = AMapFlutterLocation();
12
-  location.setLocationOption(locationOption);
9
+typedef AMAPListener = void Function (Map<String, Object>);
13 10
 
11
+void showPrivacy() {
14 12
   /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作
15 13
   ///
16 14
   /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy
@@ -31,8 +29,10 @@ Future requireLocation() async {
31 29
   ///
32 30
   /// [hasAgree] 隐私权政策是否已经取得用户同意
33 31
   AMapFlutterLocation.updatePrivacyAgree(true);
32
+}
34 33
 
35
-  // 判断定位权限
34
+// 判断定位权限
35
+Future _requirePermission() async {
36 36
   PermissionStatus _permissionGranted = await Permission.location.status;
37 37
   if (_permissionGranted == PermissionStatus.denied) {
38 38
     // 如果未允许, 尝试申请一次
@@ -41,8 +41,93 @@ Future requireLocation() async {
41 41
       throw Exception("请设置允许当前程序定位功能");
42 42
     }
43 43
   }
44
+}
44 45
 
45
-  AMapFlutterLocation.setApiKey(ConstConfig.amapApiKeys.androidKey!, ConstConfig.amapApiKeys.iosKey!);
46
+Future requireLocation(AMAPListener listener) async {
47
+
48
+  await _requirePermission();
49
+  final AMapFlutterLocation location = AMapFlutterLocation();
50
+  location.onLocationChanged().listen(listener);
51
+  location.setLocationOption(_getAMapLocationOption());
52
+  location.startLocation();
46 53
 
47 54
   return location;
48 55
 }
56
+
57
+_getAMapLocationOption() {
58
+  AMapLocationOption locationOption = new AMapLocationOption();
59
+
60
+  ///是否单次定位
61
+  locationOption.onceLocation = false;
62
+
63
+  ///是否需要返回逆地理信息
64
+  locationOption.needAddress = true;
65
+
66
+  ///逆地理信息的语言类型
67
+  locationOption.geoLanguage = GeoLanguage.DEFAULT;
68
+
69
+  locationOption.desiredLocationAccuracyAuthorizationMode =
70
+      AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
71
+
72
+  locationOption.fullAccuracyPurposeKey = "AMapLocationScene";
73
+
74
+  ///设置Android端连续定位的定位间隔
75
+  locationOption.locationInterval = 2000;
76
+
77
+  ///设置Android端的定位模式<br>
78
+  ///可选值:<br>
79
+  ///<li>[AMapLocationMode.Battery_Saving]</li>
80
+  ///<li>[AMapLocationMode.Device_Sensors]</li>
81
+  ///<li>[AMapLocationMode.Hight_Accuracy]</li>
82
+  locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
83
+
84
+  ///设置iOS端的定位最小更新距离<br>
85
+  locationOption.distanceFilter = -1;
86
+
87
+  ///设置iOS端期望的定位精度
88
+  /// 可选值:<br>
89
+  /// <li>[DesiredAccuracy.Best] 最高精度</li>
90
+  /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
91
+  /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
92
+  /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
93
+  /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
94
+  locationOption.desiredAccuracy = DesiredAccuracy.Best;
95
+
96
+  ///设置iOS端是否允许系统暂停定位
97
+  locationOption.pausesLocationUpdatesAutomatically = false;
98
+
99
+  return locationOption;
100
+}
101
+
102
+
103
+// import 'dart:io';
104
+//
105
+// import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
106
+//     show BMFMapSDK, BMF_COORD_TYPE;
107
+// import 'package:flutter_bmflocation/flutter_bmflocation.dart';
108
+// import 'package:permission_handler/permission_handler.dart';
109
+//
110
+// Future requireLocation() async {
111
+//   // 判断定位权限
112
+//   PermissionStatus _permissionGranted = await Permission.location.status;
113
+//   if (_permissionGranted == PermissionStatus.denied) {
114
+//     // 如果未允许, 尝试申请一次
115
+//     _permissionGranted = await Permission.location.request();
116
+//     if (_permissionGranted != PermissionStatus.granted) {
117
+//       throw Exception("请设置允许当前程序定位功能");
118
+//     }
119
+//   }
120
+//
121
+//   final LocationFlutterPlugin location = LocationFlutterPlugin();
122
+//
123
+//   // 设置是否隐私政策
124
+//   location.setAgreePrivacy(true);
125
+//   BMFMapSDK.setAgreePrivacy(true);
126
+//   if (Platform.isAndroid) {
127
+//     // Android 目前不支持接口设置Apikey,
128
+//     // 请在主工程的Manifest文件里设置,详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
129
+//     BMFMapSDK.setCoordType(BMF_COORD_TYPE.COMMON);
130
+//   }
131
+//
132
+//   return location;
133
+// }

+ 1
- 0
pubspec.yaml ファイルの表示

@@ -49,6 +49,7 @@ dependencies:
49 49
   amap_flutter_location: ^3.0.0
50 50
   carousel_slider: ^4.0.0
51 51
   permission_handler: ^9.2.0
52
+#  flutter_bmflocation: ^3.1.0+1
52 53
 
53 54
 
54 55