• 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';
21import router from '@ohos.router';
22
23let context: common.Context;
24
25@Component
26export struct LocationControl {
27  @State latitude: number = 0;
28  @State longitude: number = 0;
29  @State str: string = '';
30  @State message: string = '';
31  @State textTitle: string = '';
32  @State textValue: string = '';
33  @StorageLink('location') storLocation: string = '';
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.latitude = Number.parseInt(JSON.stringify(result.latitude))
73        this.longitude = Number.parseInt(JSON.stringify(result.longitude))
74        //得到当前经纬度的城市信息
75        geoLocationManager.getAddressesFromLocation({
76          "latitude": this.latitude,
77          "longitude": this.longitude,
78          "maxItems": 1
79        }).then((outcome) => {
80          this.str = JSON.stringify(outcome[0].subAdministrativeArea);
81          Logger.info('current city' + JSON.stringify(outcome))
82          this.str = this.str.replace("\"", "")
83          this.storLocation = this.str.replace("\"", "")
84        }).catch((error: string) => {
85          this.promptAction(JSON.stringify(error));
86          Logger.info('promise,getCurrentCity:error=' + JSON.stringify(error))
87        });
88
89      }).catch((error: string) => {
90        this.promptAction(JSON.stringify(error));
91        Logger.info('promise, getCurrentLocation: error=' + JSON.stringify(error));
92      });
93    } catch (error) {
94      this.promptAction(JSON.stringify(error.message));
95      let e: BusinessError = error as BusinessError;
96      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
97    }
98  }
99
100  promptAction(message: string) {
101    this.message = message;
102    try {
103      promptAction.showToast({
104        message: message,
105        duration: 60000,
106      });
107    } catch (error) {
108      let e: BusinessError = error as BusinessError;
109      Logger.error(`promise getStringByName failed, error code: ${e.code}, message: ${e.message}.`);
110    }
111  }
112
113  build() {
114    Column() {
115      LocationButton({ icon: LocationIconStyle.FULL_FILLED, buttonType: ButtonType.Circle })
116        .padding(15)
117        .onClick((event: ClickEvent, result: LocationButtonOnClickResult) => {
118          this.getLocation();
119          router.pushUrl(
120            {
121              url: 'pages/SameCityListPage',
122              params: {
123                currentIndex: 1
124              }
125            }
126          )
127        })
128    }
129    .id('lcoationButton')
130  }
131}
132