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,
    };
  }

}
相关推荐
君蓦5 小时前
Flutter 本地存储与数据库的使用和优化
flutter
problc15 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
lqj_本人1 天前
鸿蒙next选择 Flutter 开发跨平台应用的原因
flutter·华为·harmonyos
lqj_本人1 天前
Flutter&鸿蒙next 状态管理框架对比分析
flutter·华为·harmonyos
起司锅仔1 天前
Flutter启动流程(2)
flutter
hello world smile1 天前
最全的Flutter中pubspec.yaml及其yaml 语法的使用说明
android·前端·javascript·flutter·dart·yaml·pubspec.yaml
lqj_本人1 天前
Flutter 的 Widget 概述与常用 Widgets 与鸿蒙 Next 的对比
flutter·harmonyos
iFlyCai1 天前
极简实现酷炫动效:Flutter隐式动画指南第二篇之一些酷炫的隐式动画效果
flutter
lqj_本人1 天前
Flutter&鸿蒙next 中使用 MobX 进行状态管理
flutter·华为·harmonyos