鉴权检查
const result = await location.checkAuth();
if (result.success) {
console.log('鉴权成功');
}
检查定位权限状态
const status = await location.checkPermission();
console.log(`前台定位: ${status.location}, 后台定位: ${status.background}`);
关闭后台定位
开启后台定位
location.enableBackground({
bundleName: 'com.example.app',
abilityName: 'EntryAbility'
});
后台定位选项
单次定位
location.getLocation({
coorType: 'bd09ll',
isNeedAddress: true
}).then(res => {
console.log(`经度: ${res.longitude}, 纬度: ${res.latitude}`);
console.log(`地址: ${res.address}`);
}).catch(err => {
console.error(err);
})
可选options: LocationOptions定位选项
请求后台定位权限
const result = await location.requestBackgroundPermission();
if (!result.granted) {
console.log(result.message); // 提示用户去设置页面开启
}
请求定位权限
const result = await location.requestPermission();
if (result.granted) {
console.log('定位权限已授权');
}
开启持续定位
location.startLocation({
coorType: 'bd09ll',
timeInterval: 2
});
// 监听定位事件
ymx.on('location', (res) => {
console.log(`实时位置: ${res.longitude}, ${res.latitude}`);
});
// 停止定位
location.stopLocation();
可选options: LocationOptions定位选项
停止持续定位
百度定位SDK
Title
百度定位
Description
百度定位服务API
Runtime Var
ymxBaiduLocation
Runtime Factory
ymx.requirePlugin('ymx-baidu-location')