当前位置:   article > 正文

flutter 获取定位_Flutter 获取定位

get.location flutter

问题:

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

});

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号