cordova-plugin-geolocation2
v0.0.2
Published
获取地理定位
Downloads
4
Maintainers
Readme
获取安卓设备的地理定位
const getCurrentPosition = async () => {
const enable = await info.checkHMSCore();
log.debug(enable, '检查 HMS Core 可用状态');
return new Promise((resolve, reject) => {
// 使用默认插件获取经纬度
navigator.geolocation.getCurrentPosition((position) => {
resolve(translatePosition(position));
}, (error) => {
log.debug(error.message, `错误码:${error.code}`);
if (error.code === 1 && error.message === 'Illegal Access') {
reject(new Error('位置信息权限已被禁止'));
return;
}
if (enable) { // 如果HMS可用,使用HMS插件获取经纬度
log.debug('使用HMS插件定位');
const fusedClient = window.HMSLocation.getFusedLocationProviderClient();
fusedClient.getLastLocation().then(resolve).catch((err) => reject(new Error(err)));
return;
}
log.debug('使用自定义插件定位');
cordova.plugins.geolocation.getLocation({ enableHighAccuracy: true }, resolve, (err) => reject(new Error(err)));
}, {
enableHighAccuracy: false,
timeout: 1000 * 5,
maximumAge: 1000 * 10,
});
});
};