Cop*_*oad 15
接受的答案使用过时的插件,你可以使用Geolocator插件,
bool isLocationEnabled = await Geolocator().isLocationServiceEnabled();
hum*_*zed 10
先前的答案已过时。
使用最新版本的Geolocator 5.0
var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
如果禁用,我将使用此方法检查并启用Gps。
  Future _checkGps() async {
    if (!(await Geolocator().isLocationServiceEnabled())) {
      if (Theme.of(context).platform == TargetPlatform.android) {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text("Can't get gurrent location"),
              content:
                  const Text('Please make sure you enable GPS and try again'),
              actions: <Widget>[
                FlatButton(
                  child: Text('Ok'),
                  onPressed: () {
                    final AndroidIntent intent = AndroidIntent(
                        action: 'android.settings.LOCATION_SOURCE_SETTINGS');
                    intent.launch();
                    Navigator.of(context, rootNavigator: true).pop();
                  },
                ),
              ],
            );
          },
        );
      }
    }
  }
通过地理定位,您可以检查位置服务是否正常运行.它通常包括比定位包更多的可定制性.
bool serviceStatus = await _locationService.serviceEnabled();
if (service) {
    // service enabled
} else {
    // service not enabled, restricted or permission denied
}
| 归档时间: | 
 | 
| 查看次数: | 3773 次 | 
| 最近记录: |