在APP中一直使用uni.getLocation的系统定位模块获取模糊位置,用以查询同城文章。偶然发现最近升级代码后,ANDROID下uni.getLocation无法正常工作,返回error:2错误。在仔细检查后,怀疑是hbuilderx升级后的BUG。写了一段android原生代码工作正常,并在运行一次此代码获取到位置后,再使用uni.getLocation即能正常。有理由怀疑是uni.getLocation使用了getLastKnownLocation导致的,查android文档,getLastKnownLocation返回最近一次位置缓存,并不保证时效及是否有值。所以在运行这段代码后,缓存了位置值,此后uni.getLocation可以正常动作。
这是一段试验代码,并未加入超时、错误和权限检查等功能,只用于临时替换及检验uni.getLocation是否正常工作。
PS:时间原故未做更多研究,不确定是否是BUG,厂家有结果请予以告知。
__getLocation = (options) => {
const p = new Promise((resolve, reject) => {
const isNull = value => !value && reject({
errMsg: "_getLocation fail."
});
isNull(options);
isNull(options.success);
isNull(typeof options.success === "function");
const mainActivity = plus.android.runtimeMainActivity();
isNull(mainActivity);
const LOCATION_SERVICE = plus.android.getAttribute(mainActivity, "LOCATION_SERVICE");
const locationManager = plus.android.invoke(mainActivity, "getSystemService",
LOCATION_SERVICE);
plus.android.autoCollection(mainActivity);
isNull(locationManager);
const criteria = plus.android.newObject("android.location.Criteria");
isNull(criteria);
const ACCURACY_COARSE = criteria.plusGetAttribute("ACCURACY_COARSE");
plus.android.invoke(criteria, "setAccuracy", ACCURACY_COARSE);
plus.android.invoke(criteria, "setAltitudeRequired", false);
plus.android.invoke(criteria, "setBearingRequired", false);
plus.android.invoke(criteria, "setCostAllowed", false);
const POWER_LOW = criteria.plusGetAttribute("POWER_LOW");
plus.android.invoke(criteria, "setPowerRequirement", POWER_LOW);
const provider = plus.android.invoke(locationManager, "getBestProvider", criteria, true);
plus.android.autoCollection(criteria);
isNull(provider);
const location = plus.android.invoke(locationManager, "getLastKnownLocation", provider);
////怀疑是此处无值导致原函数错误
if (location) {
const longitude = plus.android.invoke(location, "getLongitude");
const latitude = plus.android.invoke(location, "getLatitude");
plus.android.autoCollection(location);
resolve({
longitude,
latitude
});
} else {
////location无值则进行监听至有值返回
let instLocationListener = null;
const locationListener = {
"onLocationChanged": function(location) {
if (location) {
const longitude = plus.android.invoke(location, "getLongitude");
const latitude = plus.android.invoke(location, "getLatitude");
plus.android.invoke(locationManager, "removeUpdates",
instLocationListener);
plus.android.autoCollection(instLocationListener);
plus.android.autoCollection(location);
resolve({
longitude,
latitude
});
}
},
"onStatusChanged": function(provider, status, extras) {
console.log(provider, status);
},
"onProviderEnabled": function(provider) {
console.log(provider + " 启用了");
},
"onProviderDisabled": function(provider) {
console.log(provider + " 关闭了");
}
}
instLocationListener = plus.android.implements("android.location.LocationListener",
locationListener);
isNull(instLocationListener);
plus.android.invoke(locationManager, "requestLocationUpdates", provider,
3000, 10,
instLocationListener);
}
});
p.then(res => options.success(res)).catch(err => options.fail && options.fail(err));
};
__getLocation({
success: res => console.log(res)
});
0 个评论
要回复文章请先登录或注册