赞
踩
问题:
Flutter getting location on Android and iOS ?
Flutter 中如何获取定位?
解决方法:
1: 添加引用到 pubspec.yaml 中
dependencies:
location: ^3.0.0
2:配置权限
Flutter1.12以上会自动配置,iOS则需要在 Info.plist中添加下面2个权限:
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
3:导入头文件并使用
import 'package:location/location.dart';
Location location = new Location(); bool _serviceEnabled; PermissionStatus _permissionGranted; LocationData _locationData; _serviceEnabled = await location.serviceEnabled(); if (!_serviceEnabled) { _serviceEnabled = await location.requestService(); if (!_serviceEnabled) { return; } } _permissionGranted = await location.hasPermission(); if (_permissionGranted == PermissionStatus.denied) { _permissionGranted = await location.requestPermission(); if (_permissionGranted != PermissionStatus.granted) { return; } } _locationData = await location.getLocation();
// 获取定位更新
location.onLocationChanged.listen((LocationData currentLocation) {
// Use current location
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。