【发布时间】:2019-02-03 05:04:20
【问题描述】:
您好,正如我在标题中所写,我该如何实现?我有定位权限但无法开启定位!
【问题讨论】:
您好,正如我在标题中所写,我该如何实现?我有定位权限但无法开启定位!
【问题讨论】:
它将打开设置。所以用户可以启用它。据我所知,这是最佳实践。
安装插件:
https://pub.dartlang.org/packages/android_intent#-installing-tab-
在你的飞镖中导入:
import 'package:android_intent/android_intent.dart';
添加方法:
void openLocationSetting() async {
final AndroidIntent intent = new AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS',
);
await intent.launch();
}
调用它完成...
【讨论】:
flutterlocation Plugin 向您显示打开 gps 的对话框,就像原生 android 一样
【讨论】:
我将使用我从 this 链接中学到的东西来指导你。
将此依赖添加到pubspec.yaml
dependencies:
location: ^3.0.0
现在,将以下包导入您的 .dart 文件
import 'package:location/location.dart';
现在在一个函数中,使用弹出框询问用户的位置,你可以这样做——
Location location = new Location();
bool _serviceEnabled;
LocationData _locationData;
以上是我们将在以下代码中使用的声明 -
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
debugPrint('Location Denied once');
}
}
现在,您可以根据需要使用上述 sn-p 使用弹出窗口进行位置请求调用,次数不限。
如果用户的位置已被授予,您可以使用以下行来存储和使用位置数据。
_locationData = await location.getLocation();
如果未授予位置,则上述行将导致程序失败,因此请确保仅在用户允许从弹出窗口访问位置时才使用上述行。
希望这会有所帮助。干杯!
【讨论】:
首先在pubspec.yaml中添加这个依赖:
location: ^1.4.0
然后,使用此函数检索您设备的当前位置:
import 'package:location/location.dart';
fetchCurrentLocation() async {
print("STARTING LOCATION SERVICE");
var location = Location();
location.changeSettings(accuracy: LocationAccuracy.POWERSAVE,interval: 1000,distanceFilter: 500);
if (!await location.hasPermission()) {
await location.requestPermission();
}
try {
await location.onLocationChanged().listen((LocationData currentLocation) {
print(currentLocation.latitude);
print(currentLocation.longitude);
latitude = currentLocation.latitude;
longitude = currentLocation.longitude;
});
} on PlatformException {
location = null;
}
}
【讨论】:
您可以使用系统对话框并要求用户启用它。检查此link 以供参考。
创建一个特定于平台的通道,并在用户想要启用它时调用它。
【讨论】:
我也这样做了,但有时不会出现位置许可的弹出通知。
我将这些行添加到以下位置:project/android/app/src/main/AndroidManifest.xml
【讨论】: