有需要直接定位到最下面。
最新改动
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);
会进入到原生,具体使用哪个方式来获取定位,从这里可以知道
- 设置属性forceAndroidLocationManager为true,则会使用android原生的LocationManager获取定位信息。
forceAndroidLocationManager为false,则会判断google服务是否可用 - google服务可用,则会使用Google Location Service的方式,这里就是FusedLocationClient封装了方法。
- google服务不可用,也会使用android原生的LocationManager获取定位信息。
public LocationClient createLocationClient(
Context context,
boolean