Przeglądaj źródła

7.30 高德定位测试

huxiaoqiang 4 lat temu
rodzic
commit
9c4321d53c

+ 1 - 0
lib/main.dart

@@ -8,6 +8,7 @@ import 'package:bbyyy/beans/template_bean_entity.dart';
 import 'package:bbyyy/beans/w_x_bean_entity.dart';
 import 'package:bbyyy/https/url.dart';
 import 'package:bbyyy/my_tools/event_bus.dart';
+import 'package:bbyyy/my_tools/loacion_util.dart';
 import 'package:bbyyy/my_tools/my_cookie.dart';
 import 'package:bbyyy/my_tools/my_tools.dart';
 import 'package:bbyyy/my_tools/order_processing/order_processing.dart';

+ 126 - 0
lib/my_tools/loacion_util.dart

@@ -0,0 +1,126 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:amap_flutter_location/amap_flutter_location.dart';
+import 'package:amap_flutter_location/amap_location_option.dart';
+import 'package:permission_handler/permission_handler.dart';
+
+import 'my_tools.dart';
+
+class LocationUtil {
+  //私有构造函数
+  LocationUtil._internal();
+
+  //保存单例
+  static LocationUtil _singleton = LocationUtil._internal();
+
+  //工厂构造函数
+  factory LocationUtil() => _singleton;
+
+  AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();
+  Map<String, Object> _locationResult;
+  StreamSubscription<Map<String, Object>> _locationListener;
+
+  initLocation() {
+    if (Platform.isIOS) {
+      requestAccuracyAuthorization();
+    }
+    _locationListener = _locationPlugin
+        .onLocationChanged()
+        .listen((Map<String, Object> result) {
+      result.forEach((key, value) {
+        print('$key---------------$value');
+      });
+      getAddressByLatitudeAndLongitude(result['latitude'], result['longitude']);
+    });
+  }
+
+  Future<void> getLocation() async {
+    var locationStatus = await Permission.location.status;
+    var cameraStatus = await Permission.camera.status;
+    if (locationStatus.isDenied) {
+      await Permission.location.request().then((value) async {
+        if (value == PermissionStatus.granted) {
+          _setLocationOption();
+          _locationPlugin.startLocation();
+        }
+      });
+    } else if (locationStatus.isGranted) {
+      _setLocationOption();
+      _locationPlugin.startLocation();
+    }
+    if (cameraStatus.isDenied) {
+      await Permission.camera.request().then((value) {});
+    }
+  }
+
+  void _setLocationOption() {
+    AMapLocationOption locationOption = new AMapLocationOption();
+
+    ///是否单次定位
+    locationOption.onceLocation = true;
+
+    ///是否需要返回逆地理信息
+    locationOption.needAddress = true;
+
+    ///逆地理信息的语言类型
+    locationOption.geoLanguage = GeoLanguage.DEFAULT;
+
+    locationOption.desiredLocationAccuracyAuthorizationMode =
+        AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
+
+    locationOption.fullAccuracyPurposeKey = "AMapLocationScene";
+
+    ///设置Android端连续定位的定位间隔
+    locationOption.locationInterval = 2000;
+
+    ///设置Android端的定位模式<br>
+    ///可选值:<br>
+    ///<li>[AMapLocationMode.Battery_Saving]</li>
+    ///<li>[AMapLocationMode.Device_Sensors]</li>
+    ///<li>[AMapLocationMode.Hight_Accuracy]</li>
+    locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
+
+    ///设置iOS端的定位最小更新距离<br>
+    locationOption.distanceFilter = -1;
+
+    ///设置iOS端期望的定位精度
+    /// 可选值:<br>
+    /// <li>[DesiredAccuracy.Best] 最高精度</li>
+    /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
+    /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
+    /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
+    /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
+    locationOption.desiredAccuracy = DesiredAccuracy.Best;
+
+    ///设置iOS端是否允许系统暂停定位
+    locationOption.pausesLocationUpdatesAutomatically = false;
+
+    ///将定位参数设置给定位插件
+    _locationPlugin.setLocationOption(locationOption);
+  }
+
+  stopLocation() {
+    ///移除定位监听
+    if (null != _locationListener) {
+      _locationListener?.cancel();
+    }
+
+    ///销毁定位
+    _locationPlugin.destroy();
+  }
+
+  void requestAccuracyAuthorization() async {
+    AMapAccuracyAuthorization currentAccuracyAuthorization =
+        await _locationPlugin.getSystemAccuracyAuthorization();
+    if (currentAccuracyAuthorization ==
+        AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
+      print("精确定位类型");
+    } else if (currentAccuracyAuthorization ==
+        AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {
+      print("模糊定位类型");
+    } else {
+      print("未知定位类型");
+    }
+  }
+}

+ 3 - 5
lib/my_tools/my_tools.dart

@@ -270,7 +270,7 @@ numberComplement(String s) {
 }
 
 String hideUID(String uid) {
-  if(uid.isEmpty||uid.length<=4){
+  if (uid.isEmpty || uid.length <= 4) {
     return '';
   }
   return uid.replaceRange(0, 4, '****');
@@ -282,6 +282,7 @@ getAddressByLatitudeAndLongitude(double latitude, double longitude) {
       .get(
           'https://restapi.amap.com/v3/geocode/regeo?output=json&location=$longitude,$latitude&key=5dcd9f0ed7d51aefb5b2f73dba1069cb&radius=10&extensions=all')
       .then((value) {
+        print('value.toString()------------${value.toString()}');
     AddressBeanEntity addressBeanEntity =
         AddressBeanEntity().fromJson(json.decode(value.toString()));
     MyCookie().location = MyLocation(
@@ -291,9 +292,6 @@ getAddressByLatitudeAndLongitude(double latitude, double longitude) {
         district: addressBeanEntity.regeocode.addressComponent.district,
         latitude: latitude,
         longitude: longitude);
-    if (addressBeanEntity.regeocode.addressComponent.province != '四川省' ||
-        addressBeanEntity.regeocode.addressComponent.city != '成都市') {
-      EventBus().emit('getLocation');
-    }
+    EventBus().emit('getLocation');
   });
 }

+ 13 - 0
lib/paegs/gang_page/create_a_gang_page/create_a_gang_page.dart

@@ -10,6 +10,8 @@ import 'package:bbyyy/https/my_request.dart';
 import 'package:bbyyy/https/url.dart';
 import 'package:bbyyy/my_tools/const.dart';
 import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
+import 'package:bbyyy/my_tools/event_bus.dart';
+import 'package:bbyyy/my_tools/loacion_util.dart';
 import 'package:bbyyy/my_tools/my_colors.dart';
 import 'package:bbyyy/my_tools/my_cookie.dart';
 import 'package:bbyyy/my_tools/my_tools.dart';
@@ -933,6 +935,17 @@ class _CreateAGangPageState extends State<CreateAGangPage> {
                         return;
                       }
                       if (!private) {
+                        EventBus().on('getLocation', (arg) {
+                          upload(_image, (r, hE) {
+                            if (!hE) {
+                              UploadImageBeanEntity data =
+                              UploadImageBeanEntity().fromJson(
+                                  json.decode(r.data.toString()));
+                              createAGang(data);
+                            }
+                          }, (e) {});
+                        });
+                        LocationUtil().getLocation();
                         // var status = await Permission.location.status;
                         // if (status.isDenied) {
                         //   await Permission.location

+ 4 - 1
lib/paegs/home_page/home_page.dart

@@ -87,6 +87,9 @@ class _HomePageState extends State<HomePage>
       _reController.requestRefresh();
     });
     EventBus().on('getLocation', (arg) {
+      if(MyCookie().location.province=='四川省'&&MyCookie().location.city=='成都市'){
+        return;
+      }
       _reController.requestRefresh();
       setState(() {});
     });
@@ -367,7 +370,7 @@ class _HomePageState extends State<HomePage>
                   return netImgs.length == 0
                       ? GestureDetector(
                     onTap: (){
-                      // MyTools().toPage(context, MapDemoPage(), (then){});
+                      MyTools().toPage(context, MapDemoPage(), (then){});
                     },
                         child: Image.asset(
                             "${imgs[index]}",

+ 4 - 99
lib/paegs/root_page/root_page.dart

@@ -11,6 +11,7 @@ import 'package:bbyyy/https/my_request.dart';
 import 'package:bbyyy/my_tools/dims.dart';
 import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
 import 'package:bbyyy/my_tools/event_bus.dart';
+import 'package:bbyyy/my_tools/loacion_util.dart';
 import 'package:bbyyy/my_tools/my_cookie.dart';
 import 'package:bbyyy/my_tools/my_datas.dart';
 import 'package:bbyyy/my_tools/my_tools.dart';
@@ -36,28 +37,14 @@ class RootPage extends StatefulWidget {
 class _RootPageState extends State<RootPage> {
   PageController pageController = PageController(initialPage: 0);
   DateTime lastPopTime;
-  AMapFlutterLocation _locationPlugin = new AMapFlutterLocation();
-  Map<String, Object> _locationResult;
 
-  StreamSubscription<Map<String, Object>> _locationListener;
 
   @override
   void initState() {
     super.initState();
     MyCookie().initP();
-
-    if (Platform.isIOS) {
-      requestAccuracyAuthorization();
-    }
-    _locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {
-      result.forEach((key, value) {
-        print('$key---------------$value');
-      });
-      setState(() {
-        _locationResult = result;
-      });
-    });
-    getLocation();
+    LocationUtil().initLocation();
+    LocationUtil().getLocation();
     Nsq.ConnectServer(
       Nsq.handlerMessage, // 接收消息处理逻辑
       // serverIp: '172.16.104.14',
@@ -97,13 +84,7 @@ class _RootPageState extends State<RootPage> {
     // TODO: implement dispose
     EventBus().off('ChangePage');
     EventBus().off('hasNoPay');
-    ///移除定位监听
-    if (null != _locationListener) {
-      _locationListener?.cancel();
-    }
-
-    ///销毁定位
-    _locationPlugin.destroy();
+    LocationUtil().stopLocation();
     super.dispose();
   }
 
@@ -209,30 +190,6 @@ class _RootPageState extends State<RootPage> {
     }, (error) {});
   }
 
-  Future<void> getLocation() async {
-    var locationStatus = await Permission.location.status;
-    var cameraStatus = await Permission.camera.status;
-    print('Permission.location.status--------------$locationStatus');
-    print('Permission.camera.status--------------$cameraStatus');
-    if (locationStatus.isDenied) {
-      await Permission.location.request().then((value) async {
-        print('Permission.location.request()-----$value');
-        if (value == PermissionStatus.granted) {
-          _setLocationOption();
-          _locationPlugin.startLocation();
-        }
-      });
-    } else if (locationStatus.isGranted) {
-      _setLocationOption();
-      _locationPlugin.startLocation();
-    }
-    if (cameraStatus.isDenied) {
-      await Permission.camera.request().then((value) {
-        print('Permission.camera.request()-----$value');
-      });
-    }
-  }
-
   //查询优惠券活动
   checkCouponEvent() {
     MyDio().query({
@@ -251,59 +208,7 @@ class _RootPageState extends State<RootPage> {
     }, (error) {});
   }
 
-  void requestAccuracyAuthorization() async {
-    AMapAccuracyAuthorization currentAccuracyAuthorization = await _locationPlugin.getSystemAccuracyAuthorization();
-    if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {
-      print("精确定位类型");
-    } else if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {
-      print("模糊定位类型");
-    } else {
-      print("未知定位类型");
-    }
-  }
-
-  void _setLocationOption() {
-    AMapLocationOption locationOption = new AMapLocationOption();
-
-    ///是否单次定位
-    locationOption.onceLocation = true;
 
-    ///是否需要返回逆地理信息
-    locationOption.needAddress = true;
 
-    ///逆地理信息的语言类型
-    locationOption.geoLanguage = GeoLanguage.DEFAULT;
 
-    locationOption.desiredLocationAccuracyAuthorizationMode = AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;
-
-    locationOption.fullAccuracyPurposeKey = "AMapLocationScene";
-
-    ///设置Android端连续定位的定位间隔
-    locationOption.locationInterval = 2000;
-
-    ///设置Android端的定位模式<br>
-    ///可选值:<br>
-    ///<li>[AMapLocationMode.Battery_Saving]</li>
-    ///<li>[AMapLocationMode.Device_Sensors]</li>
-    ///<li>[AMapLocationMode.Hight_Accuracy]</li>
-    locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
-
-    ///设置iOS端的定位最小更新距离<br>
-    locationOption.distanceFilter = -1;
-
-    ///设置iOS端期望的定位精度
-    /// 可选值:<br>
-    /// <li>[DesiredAccuracy.Best] 最高精度</li>
-    /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
-    /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
-    /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
-    /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
-    locationOption.desiredAccuracy = DesiredAccuracy.Best;
-
-    ///设置iOS端是否允许系统暂停定位
-    locationOption.pausesLocationUpdatesAutomatically = false;
-
-    ///将定位参数设置给定位插件
-    _locationPlugin.setLocationOption(locationOption);
-  }
 }