flutter 扑动:未处理异常:用户被拒绝访问设备位置

lqfhib0f  于 2023-02-09  发布在  Flutter
关注(0)|答案(5)|浏览(293)

我试图在flutter中获取用户当前位置,但它显示Map,但没有指向我的当前位置。我已经在AndriodManifest文件中添加了所有必需的权限。
这是快照

这是日志

I/Google Maps Android API(24140): Google Play services package version: 201817022
E/GoogleMapController(24140): Cannot enable MyLocation layer as location permissions are not granted
D/HostConnection(24140): HostConnection::get() New Host Connection established 0xefab0ec0, tid 25110
E/flutter (24140): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: User denied permissions to access the device's location.
E/flutter (24140): #0      MethodChannelGeolocator._handlePlatformException
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:242
E/flutter (24140): #1      MethodChannelGeolocator.getCurrentPosition
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:124
E/flutter (24140): <asynchronous suspension>
E/flutter (24140): #2      _locationState.locatepostion
package:map_app/abc.dart:26
E/flutter (24140): <asynchronous suspension>
W/System  (24140): A resource failed to call release.

电码

Completer<GoogleMapController> _controllerGoogleMap=Completer();
  late GoogleMapController newGoogleMapController;
  double mapbottompadding=0;

  GlobalKey<ScaffoldState> scaffoldkey=new GlobalKey<ScaffoldState>();
  late Position currentpositon;
  var geolocator=Geolocator();

  void locatepostion() async{
    Position position=await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    currentpositon=position;  // this is line 26, it is point before await

    LatLng latLngPosition=LatLng(position.latitude,position.longitude);

    CameraPosition cameraPosition=new CameraPosition(target: latLngPosition,zoom: 14);
    newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
  }

  static final CameraPosition googlepostion=CameraPosition(target: LatLng(37.4249,-122.0657));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: [
        GoogleMap(
          padding: EdgeInsets.only(bottom: mapbottompadding),
          mapType: MapType.normal,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
          zoomControlsEnabled: true,
          zoomGesturesEnabled: true,
           initialCameraPosition: googlepostion,
          onMapCreated: (GoogleMapController controller){
              _controllerGoogleMap.complete(controller);
              newGoogleMapController=controller;
              setState(() {
                mapbottompadding=300.0;
              });         
              locatepostion();

这是我的输出

------更新

它的工作完美的andriod手机,但不工作的模拟器.

muk1a3rh

muk1a3rh1#

LocationPermission permission;
   permission = await Geolocator.requestPermission();

首先使用此请求权限。

xe55xuns

xe55xuns2#

class GeolocatorService {
  Future<Position?> determinePosition() async {
    LocationPermission permission;
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.deniedForever) {
        return Future.error('Location Not Available');
      }
    } else {
      throw Exception('Error');
    }
    return await Geolocator.getCurrentPosition();
  }
}
bkhjykvo

bkhjykvo3#

这是geolocator中的常见问题,尤其是在您未授予设备权限时。默认情况下,它是禁用的,因此您必须在调用locatePosition()时请求它。请记住,geolocator包 Package 在google_maps_flutter下。因此,要解决位置问题,请确保执行以下操作

class _MapScreenState extends State<MapScreen> {
void locatePosition() async {
    bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
    
    await Geolocator.checkPermission();
    await Geolocator.requestPermission();
    
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    
    currentPosition = position;
    LatLng latLngPosition = LatLng(position.latitude, position.longitude);
        
    // Ask permission from device
    Future<void> requestPermission() async {
    await Permission.location.request();
    }
}
w6mmgewl

w6mmgewl4#

请检查您的权限请求。例如:您的权限状态在此按钮下;“查找我的位置”

ElevatedButton(onPressed: () async {
          PermissionStatus location = await Permission.location.request();
          locationInfo();
        }, child: const Text("Find my location")),
eufgjt7s

eufgjt7s5#

要解决此问题,请执行以下步骤:
1.向各个平台添加权限。

      • 安卓系统**:项目-〉android-〉src-〉main-〉AndroidManifest.xml在标记内添加权限

"〈android用户权限:名称=" android.权限.访问_粗略_位置/〉"

      • IOS**:项目-〉ios-〉Runner-〉Info.plist在标记内添加权限

"NS使用时的位置用法描述 NSLocationWhenInUseUsageDescription
此应用程序在打开时需要访问位置。" "
1.单击按钮时调用方法
无效getLocation()异步{
LocationPermission权限;
权限=等待地理定位器.请求权限();
位置=等待地理定位器. getCurrentPosition(所需精度:定位精度高);
打印(位置);//在这里您将获得您的纬度和经度
}
这个解决方案对我很有效,希望对你也有帮助。

相关问题