flutter - GPS 关闭消息和持续位置更新颤动
问题描述
我制作了一个应用程序,它使用 geolocator 包来检查 GPS 并获取用户的位置,我使用提供程序包来处理状态。问题是,当 GPS 关闭时,会出现一个红色屏幕,显示纬度为空,我想实现一个屏幕来告诉用户打开 GPS,并相应地更新位置。
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong/latlong.dart';
class MapState with ChangeNotifier {
bool locationServiceActive = true;
MapController _mapController;
MapController get mapController => _mapController;
static var _initialPosition;
var _lastPosition = _initialPosition;
LatLng get initialPosition => _initialPosition;
LatLng get lastPosition => _lastPosition;
MapState(){
checkGPS();
_getUserLocation();
}
checkGPS() async{
bool conn = await Geolocator().isLocationServiceEnabled();
if(conn == false){
locationServiceActive = false;
} else {
locationServiceActive = true;
}
notifyListeners();
}
void _getUserLocation() async{
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print("/////////////////////////////////////////////////////////////////////////////////position");
print(position);
_initialPosition = LatLng(position.latitude, position.longitude);
notifyListeners();
}
}
更新:-我将我的 _getUserLocation 函数更改为流,如果用户打开或关闭 gps(如果 gps 关闭,它使用最后一个已知位置)效果会更好......但它不打印语句如果位置为空,则在终端中,这很奇怪,只有当有 lat 和 lng 时!!!
这是我所做的修改...
void _getUserLocation() async{
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var geolocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);
StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen(
(Position position) {
print("/////////////////////////////////////////////////////////////////////////// position");
print(position == null ? 'Unknown' : position.latitude.toString() + ', ' + position.longitude.toString());
});
_initialPosition = LatLng(position.latitude, position.longitude);
notifyListeners();
}
解决方案
如果您想通知用户有关 GPS 的信息,您可以使用类似这样的东西
@override
void initState() {
super.initState();
initPlatformState();
location.onLocationChanged.listen((result) {
currentLocation = result;
// your code
});
});
}
void initPlatformState() async {
LocationData currentLocation;
try {
currentLocation = await location.getLocation();
error = "";
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED')
error = 'Permission Denied';
else if (e.code == 'PERMISSION_DENIED_NEVER_ASK')
error =
'Permission denied - please ask the user to enable it from the app settings';
currentLocation = null;
}
setState(() {
currentLocation = currentLocation;
});
}
在这里,如果启用,您正在收听位置流。如果不是,则会引发错误。这里使用的包是位置
推荐阅读
- python-3.x - 基于python中postgres的主键更新表
- wordpress - 将使用 ftp 下载的 wordpress 安装到我的本地主机
- qt - 在 QML 中编辑部分图像的属性
- scala - 为什么 spark 的数据集中没有 reduceBykey
- powerbi - 带有切片器的 Power BI Sankey
- maven - 如何将 maven 项目导入现有的 android 项目?
- python - 如果给出空参数,如何处理 sys.argv
- powershell - 我想从包含广告组的文件中检索父广告组
- java - 如何在 wso2product 上将 TLS 1.0 升级到 TLS 1.2
- android - Android - 如何创建像 WhatsApp 聊天这样的操作栏?