flutter定位简单工具类

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

}