【发布时间】:2018-10-04 09:33:23
【问题描述】:
我当前的应用程序使用 Location 包 (link) 来获取用户当前的经纬度,用于查找附近的设施。
这是我正在使用的代码(类似于文档中的示例)
Map<String, double> _currentLocation;
Map<String, double> _startLocation;
StreamSubscription<Map<String, double>> _locationSubscription;
String error;
bool _permission = false;
Location _location = new Location();
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
Map<String, double> location;
try {
_permission = await _location.hasPermission();
location = await _location.getLocation();
error = null;
} 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';
}
location = null;
}
setState(() {
_startLocation = location;
print("Starting coordinates: ${_startLocation["latitude"]}, ${_startLocation["longitude"]}");
});
}
@override
void initState() {
super.initState();
initPlatformState();
_locationSubscription =
_location.onLocationChanged().listen((Map<String,double> result) {
setState(() {
_currentLocation = result;
print("Current coordinates: ${_currentLocation["latitude"]}, ${_currentLocation["longitude"]}");
});
});
}
我面临的唯一问题是,每当全新安装应用程序的新 apk 时,在授予位置权限后应用程序找不到位置。
在授予位置后,我设置了一个打印语句来打印出用户的位置,但由于某种原因,它只在第一次时没有打印任何内容。重新启动应用程序后,它会很好地打印出位置。
安装后首次打开
重启应用后
有使用 Location 包的专家可以帮助我解决这个问题吗?
【问题讨论】: