• 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 { BusinessError } from '@ohos.base';
17import common from '@ohos.app.ability.common';
18import geoLocationManager from '@ohos.geoLocationManager';
19import promptAction from '@ohos.promptAction';
20import Logger from '../../utils/Logger';
21
22let context: common.Context;
23
24@Component
25export struct SecLocationButton {
26  @State message: string = '';
27  @Link location: string;
28
29  @State textTitle: string = '';
30  @State textValue: string = '';
31  @State backColor: Resource = $r('app.color.button_default_bg_color');
32  @State textColor: Resource = $r('app.color.start_window_background');
33  @State image: Resource = $r('app.media.position');
34
35  aboutToDisappear() {
36    try {
37      context.resourceManager.getStringByName("User_authorized").then(value => {
38        this.textTitle = value;
39      }).catch ((error: string) => {
40        Logger.info('promise, getCurrentLocation: error=' + error);
41      });
42    } catch (error) {
43      let e: BusinessError = error as BusinessError;
44      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
45    }
46    try {
47      context.resourceManager.getStringByName("Loading_positioning").then(value => {
48        this.textValue = value;
49      }).catch ((error: string) => {
50        Logger.info('promise, getCurrentLocation: error=' + JSON.stringify(error));
51      });
52    } catch (error) {
53      let e: BusinessError = error as BusinessError;
54      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
55    }
56  }
57
58  onCancel() {
59    Logger.info('Callback when the first button is clicked');
60  }
61
62  getLocation(): void {
63    let requestInfo: Record<string, number | boolean> = {
64      'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
65      'scenario': geoLocationManager.LocationRequestScenario.UNSET,
66      'maxAccuracy': 0,
67    };
68    try {
69      // 得到当前定位信息经纬度
70      geoLocationManager.getCurrentLocation(requestInfo).then((result) => {
71        Logger.info('current location: ' + JSON.stringify(result));
72        this.location = JSON.stringify(result);
73      }).catch ((error: string) => {
74          this.promptAction(JSON.stringify(error));
75          Logger.info('promise, getCurrentLocation: error=' + JSON.stringify(error));
76        });
77    } catch (error) {
78      this.promptAction(JSON.stringify(error.message));
79      let e: BusinessError = error as BusinessError;
80      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
81    }
82  }
83
84  promptAction( message : string ) {
85    this.message = message;
86    try {
87      promptAction.showToast({
88        message: message,
89        duration: 60000,
90      });
91    } catch (error) {
92      let e: BusinessError = error as BusinessError;
93      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
94    }
95  }
96
97  build() {
98    Row() {
99      LocationButton()
100        .onClick((event: ClickEvent, result: LocationButtonOnClickResult) => {
101          if(result == LocationButtonOnClickResult.SUCCESS){
102            this.getLocation();
103          }
104          Logger.error('SecLocationButton onClick result ' + result);
105        })
106    }
107    .id('locationButton')
108  }
109}
110