参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 设置时间类型。type为“locationChange”,表示位置变化。 |
request | LocationRequest | 是 | 设置位置请求参数。 |
callback | Callback | 是 | 接收位置变化状态变化监听。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
3301100 | Thelocation switch is off. |
3301200 | Failed to obtain the geographical location. |
import geoLocationManager from '@ohos.geoLocationManager';
let requestInfo = {'priority': 0x203, 'scenario': 0x300, 'timeInterval': 0, 'distanceInterval': 0, 'maxAccuracy': 0};
let locationChange = (location) => {
console.log('locationChanger: data: ' + JSON.stringify(location));
};
try{
geoLocationManager.on('locationChange',requestInfo,locationChange);
}catch(err){
console.error("errCode:"+err.code+",errMessage:" + err.message);
}参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 设置事件类型。type为”locationChange”,表示位置发生变化 |
callback | Callback | 否 | 需要取消订阅的回调函数。若无此参数,则取消当前类型的所有订阅。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
3301100 | The location switch is off. |
3301200 | Failed to obtain the geographical location. |
import geoLocationManager from '@ohos.geoLocationManager';
let requestInfo = {'priority': 0x203,'scenario': 0x300,'timeInterval': 0,'distanceInterval': 0,'maxAccuracy':0};
let locationChange = (location) => {
console.log('locationChanger: data: '+JSON.stringify(location));
};
try{
geoLocationManager.on('locationChange',requestInfo,locationChange);
geoLocationManager.off('locationChange',locationChange);
}catch(err){
console.error("errCode:"+err.code+",errMessage:"+err.message);
}参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 设置事件类型。type为”locationEnabledChange“,表示位置服务状态 |
callback | Callback | 是 | 接受位置服务状态变化监听。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
import geoLocationManager from '@ohos.geoLocationManager';
let locationEnabledChange = (state) =>{
console.log('locationEnableChange: '+JSON.stringify(state));
}
try{
geoLocationManager.on('locationEnabledChange',locationEnableChange);
}catch(err){
console.error("errCode:"+err.code + ",errMessage:"+err.message);
}取消订阅位置服务状态变化。
系统能力:SystemCapability.Location.Location.Core
参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 设置事件类型。type为”locationEnabledChange“,表示位置服务状态。 |
callback | Callback | 否 | 需要取消订阅的回调函数。若无此参数,则取消当前类型的所有订阅。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
import geoLocationManager from '@ohos.geoLocationManager';
let locationEnableChange = (state) =>{
console.log('locationEnabledChange: state: '+JSON.stringify(state));
}
try{
geoLocationManager.on('locationEnabledChange',locationEnabledChange);
geoLocationManager.off('locationEnabledChange',locationEnabledChange);
}catch(err){
console.error("errCode:"+err.code+",errMessage:"+err.message);
}on(type:’cachedGnssLocationsChange’,request:CachedGnssLocationsRequest,callback: Callback<Array>):void;
on(type:’satelliteStatusChange’,callback: Callback):void;
参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
type | string | 是 | 设置事件类型。type为”gnssFenceStatusChange“,表示订阅围栏事件上报。 |
request | GeofenceRequest | 是 | 围栏的配置参数。 |
want | wangAgent | 是 | 用于接受地理围栏事件上报(进出围栏)。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
3301100 | The location switch is off. |
3301600 | Failed to operate the geofence. |
import geoLocationManager from '@ohos.geoLocationManager';
import wantAgent from '@ohos.app.ability.wantAgent';
let wantAgentInfo = {
wants:[
{
bundleName: "com.example.myapplication",
abilityName: "EntryAbility",
action: "action1"
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) =>{
let requestInfo = {'priority': 0x201,'scenario':0x301,"geofence":{"latitude":26,"radius":100,"expiration":10000}};
try{
geoLocationManager.on('gnssFenceStatusChange',requestInfo,wantAgentObj);
}catch(err){
console.error("errCode:"+err.code+",errMessage:"+err.message);
}
});参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
callback | AsyncCallback | 是 | 用来接受位置信息的回调 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location Service is unavailable. |
3301100 | The location switch is off. |
3301200 | Failed to obtain the geographical location. |
import geoLocationManager from '@ohos.geoLocationManager';
let locationChange = (err,location) => {
if(err){
console.log('locationChanger: err=' + JSON.stringify(err));
}
if(location){
console.log('locationChanger: location=' + SON.stringify(location));
}
};
try{
geoLocationManager.getCurrentLocation(locationChange);
}catch(err){
console.error("errCode:"+err.code+ ",errMessage:"+err.message);
}参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
request | CurrentLocationRequest | 否 | 设置位置请求参数。 |
参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
Promise | Location | NA | 返回位置信息。 |
错误码ID | 错误信息 |
|---|---|
3301000 | Location service is unavailable. |
3301100 | The locaiton switch is off. |
3301200 | Failed to obtain the geographical location. |
import geoLocationManager from '@ohos.geoLocationManager';
let requestInfo = {'priority':0x203,'scenario': 0x300,'maxAccuracy':0};
try{
geoLocationManager.getCurrentLocation(requestInfo).then((result) =>{
console.log('current location: '+JSON.stringify(result));
})
.catch((error) =>{
console.log('promise,getCurrentLocation: error='+JSON.stringify(error));
});
}catch(err){
console.error("errCode:"+err.code + ",errMessage:"+err.message);
}