android 手机定位_android百度地图实时定位

                   
专业调查机构联系电话,18610181714。代您查询删除各类记录资料,请来电咨询。

1:使用插件geolocator,官网

2:按照官方配置,代码照抄

static Future determinePosition() async {
    bool serviceEnabled;
    LocationPermission permission;
    Position p;
    try {
    //这里使用的权限申请插件 ,请自行搜索相关文档配置获取依赖 PermissionStatus permissionStatus
= await LocationPermissions().requestPermissions(); if(PermissionStatus.granted == permissionStatus) { // Test if location services are enabled. serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // Location services are not enabled don't continue // accessing the position and request users of the // App to enable the location services. return Future.error('Location services are disabled.'); } permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.deniedForever) { // Permissions are denied forever, handle appropriately. return Future.error( 'Location permissions are permanently denied, we cannot request permissions.'); } if (permission == LocationPermission.denied) { // Permissions are denied, next time you could try // requesting permissions again (this is also where // Android's shouldShowRequestPermissionRationale // returned true. According to Android guidelines // your App should show an explanatory UI now. return Future.error('Location permissions are denied'); } } // p = await AMapLocationClient.getLocation(true); p = await Geolocator.getCurrentPosition(); print('location is get ${p}'); } else{ await LocationPermissions().openAppSettings(); } } catch (e) { ToastUtil.showToast('获取定位失败:$e'); } return FlutterLocation(p?.latitude, p?.longitude); }

以上都是官方代码,只新增了申请权限的代码(虽然加不加都一样android 手机定位,实列代码也可以申请权限)

后来自己封装了一下经纬度

class FlutterLocation {
  double lat;
  double lng;
  FlutterLocation(this.lat, this.lng);
}

正常状况下,大个别安卓手机调用这段代码都能正确获取电脑定位,但是在个别机型上面android 手机定位,特别是三星手机上(我自己遇到的状况,不代表全部)不能正确获取到电脑定位,网上搜索资料,权限申请也添加了,gps定位也开启了,死活不行。

之后查资料发现,安卓手机获得定位需要使用google服务来对gps返回信息进行解读,但是中国众多电脑厂商都屏蔽了google服务,所有电脑上仍然获得到了定位权限,也不必定能获得到恰当的定位,会经常阻塞在调用google服务里面,不报错也不返回(卡死在determinePosition.then()此处,有时catcherror能返回)

那么这些状况有两种解决方案

1:使用百度、高德api

2:对于定位精度要求不高的,可以使用webapi的方式来获得,比如:(必须自己申请key)


本文地址:http://www.tonghuachaxuns.com//chanpin/10803.html