• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import geolocation from '@ohos.geolocation';
17import { Log } from '../../utils/Log'
18import { SettingManager } from '../../setting/SettingManager'
19
20export class GeoLocation {
21  private TAG: string = '[GeoLocation]:'
22  private static instance: GeoLocation
23  private mLocation = undefined
24  private requestInfo = {'priority': 0x203, 'scenario': 0x300, 'timeInterval': 0, 'distanceInterval': 0, 'maxAccuracy': 0}
25  private locationChange = (location) => {//删掉参数err及相关逻辑
26//    Log.info(`[GeoLocation]: locationChange err: ${err}`)
27//    if (!err) {
28      Log.info(`[GeoLocation]: locationChange: ${location}`)
29      SettingManager.getInstance().setCurGeoLocation(location)
30//    }
31  }
32
33  private Constructor() {}
34
35  public static getInstance() {
36    if (!GeoLocation.instance) {
37      GeoLocation.instance = new GeoLocation()
38    }
39    return GeoLocation.instance
40  }
41
42  public async on() {
43    Log.info(`${this.TAG} on E`)
44    if (!SettingManager.getInstance().getSaveGeoLocation()) {
45      Log.info(`${this.TAG} on geo setting off, X`)
46      return
47    }
48    geolocation.isLocationEnabled().then((result) => {
49      Log.info(`${this.TAG} isLocationEnabled: ${result}`)
50      if (result) {
51        var curRequestInfo = {'priority': 0x203, 'scenario': 0x300, 'maxAccuracy': 1000}
52        geolocation.getCurrentLocation(curRequestInfo).then((result) => {
53          Log.info(`${this.TAG} on getCurrentLocation result: ${JSON.stringify(result)}`)
54          SettingManager.getInstance().setCurGeoLocation(result)
55        }).catch((err) => {
56          Log.info(`${this.TAG} on getCurrentLocation err result: ${JSON.stringify(err)}`)
57        })
58        geolocation.on('locationChange', this.requestInfo, this.locationChange)
59      }
60    })
61    Log.info(`${this.TAG} on X`)
62  }
63
64  public async off() {
65    Log.info(`${this.TAG} off E`)
66    if (!SettingManager.getInstance().getSaveGeoLocation()) {
67      Log.info(`${this.TAG} off geo setting off X`)
68      return
69    }
70    geolocation.off('locationChange', this.locationChange)
71    Log.info(`${this.TAG} off X`)
72  }
73}