【问题标题】:What is wrong with my setup of flutter geolocator: ^6.1.7?我的颤振地理定位器设置有什么问题:^6.1.7?
【发布时间】:2020-11-23 02:48:25
【问题描述】:

顺便说一句 - 事情在 IOS 上按预期工作。 我遇到的问题是在 Android (Nexus 5 API 30) 上运行

我已仔细按照instructions 进行设置,特别注意 gradle.properties 和 "android/app/build.gradle" AndroidManifest.xml 也更新为包含

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION 
uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION

应用启动时,会提示是否允许定位服务。

我什至可以检查应用上的定位服务是否确实启用了-

这里是调用地理定位的代码...

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {

  void initState() {
    super.initState();
    getLocation();
  }
  void getLocation() async {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    print(position);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () {
            getLocation();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

当我运行代码时,有很长的异常列表,前几个是:

E/flutter (21698): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: The location service on the device is disabled.
E/flutter (21698): #0      MethodChannelGeolocator._handlePlatformException (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:192:9)
E/flutter (21698): #1      MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:121:7)
E/flutter (21698): <asynchronous suspension>
E/flutter (21698): #2      Geolocator.getCurrentPosition (package:geolocator/geolocator.dart:258:35)

【问题讨论】:

  • 检查设备是否开启定位功能

标签: android flutter geolocation


【解决方案1】:
try{
 await getLocation();
} catch(e,s){
 //handle errors
}

有些 Android 模拟器有这个确切的问题(错误),请尝试不同的模拟器或设备

【讨论】:

    【解决方案2】:

    初始化变量

    Position _currentPosition;
    

    代码

    _getCurrentLocation() async{
     await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
            .then((Position position) {
          setState(() {
            _currentPosition = position;
            print(_currentPosition); 
        
          });
    
        
        }).catchError((e) {
          print(e);
        });
      }
    

    运行flutter clean从手机/模拟器卸载应用并重启手机/模拟器 然后在接受许可后再次运行应用程序,它将自动要求启用位置对话框,如果出现问题,请尝试使用 location

    【讨论】:

      【解决方案3】:

      添加 forceAndroidLocationManager:true 使它对我有用。

      position = await Geolocator.getCurrentPosition(
                    desiredAccuracy: LocationAccuracy.medium,
                    forceAndroidLocationManager: true)
                .timeout(Duration(seconds: 10));
      

      【讨论】:

        猜你喜欢
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        • 2023-03-25
        • 1970-01-01
        • 2023-03-13
        相关资源
        最近更新 更多