关于flutter-geolocator库使用问题,解决android使用LocationManager定位问题


有需要直接定位到最下面。

最新改动

2022年7月7日更新,如果getCurrentPosition获取不到数据,可以尝试先调用getLastKnownPosition。
2022年1月28日更新,geolocator官方库在v8+的版本中适配了android12,并且执行方法有变动, 基于源码v8.0.3修改 ,分支geolocator_v8.0.3_20220121,引入和修改源码位置可以参考以下内容。

相关知识

  • geolocator flutter一个比较好用的定位库
  • android原生提供的LocationManager,常用的定位库
  • 谷歌定位服务Google Location Service,需要引入google服务
    已经具备定位权限,并用户同意授权。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

问题

import 'package:geolocator/geolocator.dart';

Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

在使用flutter-geolocator遇到的问题。有的手机获取定位很快,有的手机半天没有定位结果,定位很慢。

现象

查看flutter-geolocator源码,发现内部使用了两种定位方式。一种是android原生提供的LocationManager,另一种是谷歌定位服务Google Location Service,需要引入google服务。
1.在使用Google Location Service的方式获取定位,没有翻墙,没有回调。
2.在使用原生LocationManager的方式获取定位,有两种情况,一种是网络定位、一种是GPS定位。使用网络定位,但没有连接网络,没回调。使用PGS定位,在室内测试没回调。

分析问题

在Flutter调用Geolocator.getCurrentPosition方法

Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

会进入到原生,具体使用哪个方式来获取定位,从这里可以知道

  1. 设置属性forceAndroidLocationManager为true,则会使用android原生的LocationManager获取定位信息。
    forceAndroidLocationManager为false,则会判断google服务是否可用
  2. google服务可用,则会使用Google Location Service的方式,这里就是FusedLocationClient封装了方法。
  3. google服务不可用,也会使用android原生的LocationManager获取定位信息。
public LocationClient createLocationClient(
      Context context,
      boolean
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值