设备与位置
设备与位置
获取设备类型
matrix.getDeviceType(params)
获取当前运行环境的设备类型。此接口无需 config 即可调用。
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| onSuccess | Function | 必填 | 成功回调 |
| onFail | Function | 必填 | 失败回调 |
返回参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| deviceType | String | 设备类型,可选值见下表 |
设备类型值
| 值 | 说明 |
|---|---|
| android | Android 设备 |
| ios | iOS 设备(iPhone/iPad) |
| windows | Windows 桌面 |
| macos | macOS 桌面 |
| linux | Linux 桌面 |
| web | Web 浏览器 |
| unknown | 未知平台 |
调用示例
matrix.getDeviceType({
onSuccess: function(data) {
console.log('设备类型:', data.deviceType);
if (data.deviceType === 'android') {
// Android 特定逻辑
} else if (data.deviceType === 'ios') {
// iOS 特定逻辑
}
},
onFail: function(error) {
console.error('获取失败:', error.message);
}
});
获取当前群 ID
matrix.getCurrentGroupId(params)
获取打开当前应用时由路由传入 WebView 的群 ID。此接口属于默认允许的接口,无需调用 config,也无需在 jsApiList 中声明。
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| onSuccess | Function | 可选 | 成功回调,直接接收包含 groupId 的数据对象,建议传入 |
| onFail | Function | 可选 | 失败回调,通过 error.code 和 error.message 获取错误信息,建议传入 |
无业务参数,仅接受回调函数;不要传入额外字段,否则返回 -3 INVALID_PARAMS。
返回参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| groupId | String | 当前 WebView 实例的群 ID;从工作台等非群入口打开时返回空字符串 '' |
onSuccess(data) 收到的是 { groupId: '12345' } 或 { groupId: '' },使用 data.groupId 读取,不需要再访问 data.data。空字符串是成功结果,不会触发失败回调。
调用示例
var currentGroupId = '';
// 获取并保存当前页面的群上下文,供后续业务使用。
function loadCurrentGroupId() {
matrix.getCurrentGroupId({
onSuccess: function(data) {
currentGroupId = data.groupId;
if (currentGroupId) {
console.log('当前群 ID:', currentGroupId);
} else {
console.log('当前应用不是从群入口打开');
}
},
onFail: function(error) {
console.error('获取群 ID 失败:', error.code, error.message);
}
});
}
loadCurrentGroupId();
获取当前位置
matrix.getLocation(params)
获取设备调用时的前台位置。此接口无需 config 即可调用。
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | String | 可选 | 返回坐标系,仅允许 WGS84 或 GCJ-02,默认 WGS84 |
| onSuccess | Function | 必填 | 成功回调 |
| onFail | Function | 必填 | 失败回调 |
返回参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| latitude | Number | 指定坐标系的纬度 |
| longitude | Number | 指定坐标系的经度 |
| accuracy | Number | 定位水平精度,单位米 |
| type | String | 实际返回的坐标系 |
调用示例
matrix.getLocation({
type: 'GCJ-02',
onSuccess: function(data) {
console.log('当前位置:', data.latitude, data.longitude);
console.log('坐标系:', data.type);
console.log('精度:', data.accuracy, '米');
},
onFail: function(error) {
console.error('定位失败:', error.message);
}
});