Flutter,如何在应用程序启动后才能获取用户位置?
Flutter, how can I get user location only once the application started?
我目前正在尝试在开始时获取用户位置一次,并将相机聚焦在该位置一次,然后我希望它停止但无法这样做。代码部分如下。
bool element = true;
void _onMapCreated(GoogleMapController _cntrl){
setState(() {
_ifEnabled();
_ifPermissioned();
_getLocation();
_controller = _cntrl;
_location.onLocationChanged.listen((l){
_controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(l.latitude,l.longitude),
zoom: 15
),
),
);
});
_location.onLocationChanged.skipWhile((element) => false);
});
}
根据 https://pub.dev/packages/location
上的官方文档
onLocationChanged
侦听位置并在位置更改时自动更新,所以你应该使用什么
getLocation()
getLocation()
Allow to get a one time position of the user. It will try to request permission if not granted yet and will throw a PERMISSION_DENIED error code if permission still not granted.
我目前正在尝试在开始时获取用户位置一次,并将相机聚焦在该位置一次,然后我希望它停止但无法这样做。代码部分如下。
bool element = true;
void _onMapCreated(GoogleMapController _cntrl){
setState(() {
_ifEnabled();
_ifPermissioned();
_getLocation();
_controller = _cntrl;
_location.onLocationChanged.listen((l){
_controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(l.latitude,l.longitude),
zoom: 15
),
),
);
});
_location.onLocationChanged.skipWhile((element) => false);
});
}
根据 https://pub.dev/packages/location
上的官方文档onLocationChanged
侦听位置并在位置更改时自动更新,所以你应该使用什么
getLocation()
getLocation() Allow to get a one time position of the user. It will try to request permission if not granted yet and will throw a PERMISSION_DENIED error code if permission still not granted.