flutter定位简单工具类

dart 复制代码
import 'package:permission_handler/permission_handler.dart';

class PermissionUtil {
  /// 获取用户定位权限
  static Future<bool> getLocationStatus() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.location,
    ].request();

    return statuses[Permission.location].isGranted;
  }

  //判断是否已经获取了位置权限授权
  static Future<bool> judgeLocationAuth() async {
    var status = await Permission.locationWhenInUse.status;
    if (status == PermissionStatus.granted) {
      return true;
    }else {
      return false;
    }
  }

//判断手机是否开启了定位服务
 static Future<bool> serviceEnabled() async {
   bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
   return serviceEnabled;
  }

//1.获取位置信息
Future<Position> _determinePosition() async {
    bool serviceEnabled;
    LocationPermission permission;

    //判断手机是否开启定位服务
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    //位置服务是否启用
    if (!serviceEnabled) {

      return Future.error('Location services are disabled.');
    }

    //获取位置位置权限

    /*bool locationAuth = await _locationAuthDialog();
    if(locationAuth){

    }*/
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error('Location permissions are denied');
      }
    }
    //位置权限是否永久拒绝
    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }

    return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best,forceAndroidLocationManager: true);
  }

//2.从经纬度编码成地址
getAddressFromLatLng() async{
    Position position = await _determinePosition();
    print('纬度:${position.latitude}   经度:${position.longitude} ');
    List<Placemark> placeMark = await placemarkFromCoordinates(position.latitude, position.longitude);
    print('地址:${placeMark[0].administrativeArea} ${placeMark[0].locality}  ${placeMark[0].subLocality} ${placeMark[0].subAdministrativeArea} ${placeMark[0].thoroughfare} ${placeMark[0].subThoroughfare} ${placeMark[0].name} ${placeMark[0].name}');
    CommonConfig.lat = position.latitude;
    CommonConfig.lng = position.longitude;
    CommonConfig.currentCityName = placeMark[0].locality;
    return {
      "provinceName": placeMark[0].administrativeArea,
      "cityName": placeMark[0].locality,
      "districtName": placeMark[0].subLocality,
    };
  }

}
相关推荐
江上清风山间明月1 分钟前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
yuanlaile16 小时前
纯Dart Flutter库适配HarmonyOS
flutter·华为·harmonyos·flutter开发鸿蒙·harmonyos教程
yuanlaile16 小时前
Flutter开发HarmonyOS 鸿蒙App的好处、能力以及把Flutter项目打包成鸿蒙应用
flutter·华为·harmonyos·flutter开发鸿蒙
zacksleo17 小时前
鸿蒙原生开发手记:04-一个完整元服务案例
flutter
jcLee952 天前
Flutter/Dart:使用日志模块Logger Easier
flutter·log4j·dart·logger
tmacfrank2 天前
Flutter 异步编程简述
flutter
tmacfrank2 天前
Flutter 基础知识总结
flutter
叫我菜菜就好2 天前
【Flutter_Web】Flutter编译Web第三篇(网络请求篇):dio如何改造方法,变成web之后数据如何处理
前端·网络·flutter
AiFlutter2 天前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
m0_748247803 天前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter