打开定位服务而不导航到设置页面?飞镖
Posted
技术标签:
【中文标题】打开定位服务而不导航到设置页面?飞镖【英文标题】:Turn on location services without navigating to settings page ? Flutter Dart 【发布时间】:2019-06-21 05:01:45 【问题描述】:我们正在从 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();
【问题讨论】:
查看simple_permissions 包。 【参考方案1】:在屏幕主体中添加地图
@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)));
此代码将在主屏幕中初始化谷歌地图,如果位置被关闭,则会弹出一个警告框询问权限,然后它将自动打开
【讨论】:
【参考方案2】:您可以使用location 包,您只需在您的AndroidManifest.xml 和Info.plist 文件中添加所需的权限,将在弹出窗口中询问权限。
【讨论】:
启用位置权限和位置服务是两件事以上是关于打开定位服务而不导航到设置页面?飞镖的主要内容,如果未能解决你的问题,请参考以下文章
如何在允许用户导航到其他页面的同时使用 strophe 保持聊天会话打开?