【问题标题】:The location service on the device is disabled [Flutter geolocator v6.1.4]设备上的定位服务被禁用 [Flutter geolocator v6.1.4]
【发布时间】:2021-02-27 12:33:17
【问题描述】:

在尝试获取当前位置时,我总是会收到该消息。我在真实设备上运行我的应用程序。我按照geolocator plugin 此处的文档说明进行操作。起初它获取位置,然后在退出应用程序并重新启动它后,它说

设备上的定位服务已禁用

为什么即使我已经启用了定位服务,我也会收到这个错误?

class LocationController extends GetxController{

   var position = Position().obs;
   var lat = 0.00.obs;
   var long = 0.00.obs;
 Future getLocation() async {
   try{

       position.value = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

       lat.value = position.value.latitude;
       long.value = position.value.longitude;
     } catch (e){
       print(e);
     }
  }
}

打电话...

locationController.getLocation();

【问题讨论】:

    标签: android flutter dart geolocation


    【解决方案1】:

    下载和导入插件

    geolocatorgeocoding

    import 'package:geocoding/geocoding.dart';
    import 'package:geolocator/geolocator.dart';
    

    在你的 AndroidManifest 添加权限

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    

    初始化变量

    Position _currentPosition;
    String _currentAddress = "";
    

    当前位置函数

    _getCurrentLocation() async {
    
        await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) {
      setState(() {
       //Pass the lat and long to the function
        _getAddressFromLatLng(position.latitude, position.longitude);
    
      });
    }).catchError((e) {
      print(e);
    });
      }
    

    _getAddressFromLatLng

     _getAddressFromLatLng(double latitude, double longitude) async {
        try {
          List<Placemark> p = await placemarkFromCoordinates(latitude, longitude);
    
          Placemark place = p[0];
    
          setState(() {
            _currentAddress = "${place.locality}";
            print(_currentAddress);       
        
          });
        } catch (e) {
          print(e);
        }
      }
    

    如果出现上述问题只需运行flutter clean,卸载应用程序然后重新启动手机并再次运行项目

    【讨论】:

    • 如何将经纬度转换成人类可读的地址?
    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2011-12-18
    • 2020-06-18
    相关资源
    最近更新 更多