gpt4 book ai didi

dart - 在不导航到设置页面的情况下打开位置服务? Dart

转载 作者:IT王子 更新时间:2023-10-29 06:35:11 26 4
gpt4 key购买 nike

我们正在从 迁移到 Flutter。我们将此线程用于 Turn on location services without navigating to settings page

这在 Flutter 中如何实现?

导航到设置的当前临时代码:

  Future _getCurrentLocation() async {
Position position;
try {
position = await Geolocator().getCurrentPosition(
desiredAccuracy: LocationAccuracy.bestForNavigation);
} on PlatformException catch(e){
print(e.code);
if(e.code =='PERMISSION_DISABLED'){
print('Opening settings');
await openLocationSetting();
try {
position = await Geolocator().getCurrentPosition(
desiredAccuracy: LocationAccuracy.bestForNavigation);
} on PlatformException catch(e){
print(e.code);
}
}
}

setState(() {
// _center = LatLng(currentLocation['latitude'], currentLocation['longitude']);
_center = LatLng(position.latitude, position.longitude);
});
}
Future openLocationSetting() async {
final AndroidIntent intent = new AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS',
);
await intent.launch();

}

最佳答案

在屏幕正文中添加 map

@override
Widget build(BuildContext context) {
return Scaffold(
body: Builder(
builder: (context) => Stack(
children: [
GoogleMap(
scrollGesturesEnabled: true,
tiltGesturesEnabled: true,
rotateGesturesEnabled: true,
mapToolbarEnabled: true,
mapType: MapType.normal,
onTap: _placeMarker,
onMapCreated: _onMapCreated,
markers: Set.from(myMarker),
initialCameraPosition: CameraPosition(
target: _center,
zoom: 5.0,
),
),
Padding(
padding: (EdgeInsets.symmetric(horizontal: 16, vertical: 40)),
child: Align(
alignment: Alignment.topRight,
child: Column(
children: [
CircleAvatar(
radius: 30,
child: IconButton(
onPressed: () {
navigate(context);
},
icon: Icon(
Icons.done,
color: Colors.white,
),
),
),
SizedBox(
height: 10,
),
CircleAvatar(
radius: 30,
child: IconButton(
onPressed: **getLocation**,
icon: Icon(
Icons.location_on,
color: Colors.white,
),
),
)
],
),
),
),
Padding(
padding: (EdgeInsets.symmetric(horizontal: 16, vertical: 40)),
child: Align(
alignment: Alignment.topLeft,
child: Column(
children: [
CircleAvatar(
radius: 30,
child: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.chevron_left,
color: Colors.white,
),
),
),
SizedBox(
height: 10,
),
],
),
),
)
],
),
));
}


getLocation() async {
position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
print(position);
mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 18,
tilt: 36)));
}

此代码将初始化主屏幕中的 Google map ,如果位置被关闭,则会弹出一个警告框,要求获得许可,然后它会自动打开

关于dart - 在不导航到设置页面的情况下打开位置服务? Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54397823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com