首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter的Geolocator包返回纬度和经度值的空值。为什么?[Android]

Flutter的Geolocator包返回纬度和经度值的空值。为什么?[Android]
EN

Stack Overflow用户
提问于 2022-06-17 15:37:43
回答 3查看 436关注 0票数 0

这是我的第一个StackOverflow问题,作为一个新手,我正在学习飞镖。

我已经被介绍给geolocator包,并且我一直在使用最新版本,即8.2.1版本。我还在使用Android和它的Android模拟器。

所面临的挑战是获得纬度和经度值,这些值将来自地理坐标获取的位置数据。

经过一些测试后,结果是启用了位置服务,并授予了要注意的位置数据的权限,但尽管如此,地理位置数据仍未被geolocator检索。

我花了一些时间在模拟器的设置中,并设法在加州的圣何塞设置了一个位置,我希望geolocator能够找到并使用它,但是这样做并没有什么区别。

控制台的回应(似乎很重要)是“未来未完成”:

✓构建了build/app/outputs/颤振-apk/app-调试器。安装构建/应用程序/输出/颤振-apk/app.apk.D/FlutterGeolocator( 6027):将Geolocator附加到活动D/FlutterGeolocator( 6027):创建服务。D/FlutterGeolocator( 6027):绑定到定位服务。D/FlutterGeolocator( 6027):地理前景服务连接D/FlutterGeolocator( 6027):初始化Geolocator服务调试服务侦听ws://127.0.0.1:64162/f6U62iu6OXc=/ws同步文件到gphone64 x86 64.I/颤振( 6027):当前,模拟器的位置服务状态= true。D/CompatibilityChangeReporter( 6027):Compat id报告: 78294732;UID 10149;状态:启用I/flutter ( 6027):当前位置权限状态= LocationPermission.whileInUse。I/颤振( 6027):0:00后的TimeoutException :05.000000:05.000000:未来未完成

我的守则:

代码语言:javascript
运行
复制
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(ScreenView());
}

class ScreenView extends StatefulWidget {
  double? latitude;
  double? longitude;

  ScreenView({this.latitude, this.longitude});

  void locationHereIs() async {
    await locationServicesStatus();
    await checkLocationPermissions();
    try {
      Position position = await Geolocator.getCurrentPosition(
              desiredAccuracy: LocationAccuracy.low)
          .timeout(Duration(seconds: 5));
      print(position);
    } catch (e) {
      print(e);
    }
  }

  Future<void> checkLocationPermissions() async {
    LocationPermission permission = await Geolocator.requestPermission();
    print('Current Location Permission Status = $permission.');
  }

  void checkLocationSettings() async {
    await Geolocator.openLocationSettings();
  }

  Future<void> locationServicesStatus() async {
    bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
    print(
        'Currently, the emulator\'s Location Services Status = $isLocationServiceEnabled.');
  }

  @override
  State<ScreenView> createState() => _ScreenViewState();
}

class _ScreenViewState extends State<ScreenView> {
  @override
  void initState() {
    ScreenView().locationHereIs();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

如果你能帮我了解出了什么问题,这样我就能解决问题,得到结果,那就太完美了。在您的答复中,请假定我使用了complileSdkVersion 32miniSdkVersion 23targetSdkVersion 32

(谢谢:)

EN

回答 3

Stack Overflow用户

发布于 2022-06-17 17:09:22

使用此代码修复它:

代码语言:javascript
运行
复制
Future<Position> _getGeoLocationPosition() async {
    bool serviceEnabled;
    LocationPermission permission;
    // Test if location services are enabled.
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      await Geolocator.openLocationSettings();
      return Future.error('Location services are disabled.');
    }
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        print('Location permissions are denied');
        return Future.error('Location permissions are denied');
      }
    }
    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.
      print('Location permissions are permanently denied, we cannot request permissions.');
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }
    // When we reach here, permissions are granted and we can
    // continue accessing the position of the device.
    return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  }

结果:

代码语言:javascript
运行
复制
Position position = await _getGeoLocationPosition();
票数 0
EN

Stack Overflow用户

发布于 2022-06-18 10:41:15

我已经找到了一个解决方案和一个合理的解释,为什么current-position数据将由geolocator检索而不是

  • android模拟器

原因是android模拟器不是真正的设备,缺乏在真正的android设备中找到的功能水平;android模拟器不能接受geolocator的当前位置功能。

不过,Android仿真器确实接受geolocator的lastKnownLocation函数,并且在模拟器的位置设置中设置的位置将被geolocator通过其lastKnownLocation函数来注意和确认。

我相信这个发现能帮助每个使用geolocator的人依赖于android仿真器:)

Dart代码示例:

代码语言:javascript
运行
复制
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(ScreenView());
}

class ScreenView extends StatefulWidget {
  double? latitude;
  double? longitude;

  ScreenView({this.latitude, this.longitude});

  void lastKnownPosition() async {
    await locationServicesStatus();
    await checkLocationPermissions();
    try {
      Position? position = await Geolocator.getLastKnownPosition();
      print(position);
    } catch (e) {
      print(e);
    }
  }

  void locationHereIs() async {
    await locationServicesStatus();
    await checkLocationPermissions();
    try {
      Position position = await Geolocator.getCurrentPosition(
              desiredAccuracy: LocationAccuracy.low)
          .timeout(Duration(seconds: 28));
      print(position);
    } catch (e) {
      print(e);
    }
  }

  Future<void> checkLocationPermissions() async {
    LocationPermission permission = await Geolocator.requestPermission();
    print('Current Location Permission Status = $permission.');
  }

  void checkLocationSettings() async {
    await Geolocator.openLocationSettings();
  }

  Future<void> locationServicesStatus() async {
    bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
    print(
        'Currently, the emulator\'s Location Services Status = $isLocationServiceEnabled.');
  }

  @override
  State<ScreenView> createState() => _ScreenViewState();
}

class _ScreenViewState extends State<ScreenView> {
  @override
  void initState() {
    ScreenView().lastKnownPosition();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

lastKnownLocation输出控制台

代码语言:javascript
运行
复制
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app.apk...
Debug service listening on ws://127.0.0.1:52586/aA04hHZ7dIg=/ws
Syncing files to device sdk gphone64 x86 64...
I/flutter (11353): Currently, the emulator's Location Services Status = true.
D/CompatibilityChangeReporter(11353): Compat change id reported: 78294732; UID 10149; state: ENABLED
I/flutter (11353): Current Location Permission Status = LocationPermission.whileInUse.
I/flutter (11353): Latitude: 37.333333333333336, Longitude: -121.89206891099967

结论:一直以来都有一种怀疑,认为地理标志包是有缺陷的,没有任何解释。上面的演示和解释说明geolocator在android模拟器中运行良好,应该仍然是android开发人员的最爱。

票数 0
EN

Stack Overflow用户

发布于 2022-11-16 07:26:18

geolocator 8.2.1我不知道为什么API 28 Pie仿真器工作,但例如“设置位置”按钮-对我来说,不工作在模拟器API版本29 30 31 ->循环D/FlutterGeolocator(26734):将地理标志附加到活动D/FlutterGeolocator(26808):将Geolocator附加到活动中。我将工作在API模拟器28派。也许这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72661741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档