【问题标题】:how to activate GPS after get user location permission once in flutter如何在获得用户位置许可后激活 GPS
【发布时间】:2020-02-06 06:09:43
【问题描述】:

我想在用户打开应用程序时自动启用 GPS 定位,用户安装应用程序时授予用户定位权限。如何在颤振中自动激活位置?

Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
Future<Position> getCurrentLocation() async {
    Position position = await geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    return position;
  }

【问题讨论】:

  • 与我们分享您尝试自动激活 GPS 的代码。

标签: asynchronous flutter dart gps location


【解决方案1】:

使用 pub.dev 中的location lib,您可以找到所需的解决方案。使用您的代码从用户那里获得位置许可后,只需添加 2 行。这里也是一个示例代码。

import 'package:location/location.dart' as loc;

Future<Position> getCurrentLocation() async {
    loc.Location locationR = loc.Location();

    if (!await locationR.serviceEnabled()) {
      locationR.requestService();
    }
    Position position = await geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    return position;
  }

这 2 行代码检查您的 GPS 是否打开/关闭。如果关闭,则它会提供一个提示对话框以打开 GPS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多