| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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:flutter/src/widgets/framework.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(BuildContext context) {
- if (Platform.isIOS) {
- requestAccuracyAuthorization();
- }
- _locationListener = _locationPlugin
- .onLocationChanged()
- .listen((Map<String, Object> result) {
- result.forEach((key, value) {
- // print('$key---------------$value');
- });
- getAddressByLatitudeAndLongitude(double.parse('${result['latitude']}'), double.parse('${result['longitude']}'),context);
- });
- }
- 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 {
- _locationPlugin.startLocation();
- 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("未知定位类型");
- }
- }
- }
|