关于华为鸿蒙系统定位,provider是fused调用getLastKnownLocation返回值为null的问题

关于华为鸿蒙系统定位,provider是fused调用getLastKnownLocation返回值为null的问题

测试设备:荣耀V30PRO 鸿蒙3.0.0.208


问题描述

获取最佳的Provider

		criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置精确度
		criteria.setAltitudeRequired(true); // 设置请求海拔
		criteria.setBearingRequired(true); // 设置请求方位
		criteria.setCostAllowed(true); // 设置允许运营商收费
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		if (isUsingMockGPS) {
			provider = GPS_MOCK_PROVIDER;
		} else {
			provider = lm.getBestProvider(criteria, true);
		}

然后requestLocationUpdates使用该provider

	lm.requestLocationUpdates(provider, 1000, 1, mylocationListener);
/**
	 *循环获取GPS、NETWORK、PASSIVE定位方式
	 * */
	private String currentBestProvider = "";
	private Location getLastKnownLocation() {
		//mLocationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
		List<String> providers = lm.getProviders(true);
		Location bestLocation = null;
		for (String provider : providers) {
			@SuppressLint("MissingPermission")
			Location location = lm.getLastKnownLocation(provider);
			if (location == null) {
				continue;
			}
			if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) {
				bestLocation = location;
				currentBestProvider = provider;
			}
		}
		return bestLocation;
	}

日志打印结果:

requestLocationUpdates的provider是fused
调用getLastKnownLocation返回的值是null

解决方案:

把requestLocationUpdates传入的provider改为gps

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, mylocationListener);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值