• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Flutter 获取定位

Flutter cps12345 11个月前 (12-12) 1823次浏览 0个评论
问题:

Flutter getting location on Android and iOS ?

Flutter 中如何获取定位?

解决方法:

使用插件:location

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
});
喜欢 (7)

您必须 登录 才能发表评论!