diff --git a/AppScope/resources/base/element/string.json b/AppScope/resources/base/element/string.json index 2acf1c0..bac90fe 100644 --- a/AppScope/resources/base/element/string.json +++ b/AppScope/resources/base/element/string.json @@ -2,7 +2,7 @@ "string": [ { "name": "app_name", - "value": "HM4Demo" + "value": "HMDemo" } ] } diff --git a/app/src/main/ets/MyApp.ets b/app/src/main/ets/MyApp.ets index 2478c58..573ff66 100644 --- a/app/src/main/ets/MyApp.ets +++ b/app/src/main/ets/MyApp.ets @@ -1,5 +1,5 @@ import { Context } from '@ohos.arkui.UIContext'; -import window from '@ohos.window'; +import { window } from '@kit.ArkUI'; export class MyApp { diff --git a/app/src/main/ets/appability/AppAbility.ets b/app/src/main/ets/appability/AppAbility.ets index 46ea429..0b8c0b0 100644 --- a/app/src/main/ets/appability/AppAbility.ets +++ b/app/src/main/ets/appability/AppAbility.ets @@ -19,19 +19,19 @@ export default class AppAbility extends UIAbility { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); } - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - MyApp.initAbility(this.context); - windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); - return; - } - MyApp.initWindow(windowStage.getMainWindowSync()); - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); - }); - } +onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + MyApp.initAbility(this.context); + windowStage.loadContent('pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + MyApp.initWindow(windowStage.getMainWindowSync()); + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + }); +} onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources diff --git a/app/src/main/ets/pages/Index.ets b/app/src/main/ets/pages/Index.ets index ed1c6b8..7c8051e 100644 --- a/app/src/main/ets/pages/Index.ets +++ b/app/src/main/ets/pages/Index.ets @@ -65,6 +65,12 @@ struct Index { {name: "MVVM", page: 'pages/mvvm/HomePage'}, {name: "SetLanguage", page: 'pages/system/SetLanguagePage'} ] + }, + { + name: 'Map', + items: [ + {name: 'Map', page: 'pages/map/MapPage'} + ] } ]; diff --git a/app/src/main/ets/pages/map/MapPage.ets b/app/src/main/ets/pages/map/MapPage.ets new file mode 100644 index 0000000..7d6c66f --- /dev/null +++ b/app/src/main/ets/pages/map/MapPage.ets @@ -0,0 +1,95 @@ +import { abilityAccessCtrl, bundleManager, common, PermissionRequestResult } from '@kit.AbilityKit'; +import { geoLocationManager } from '@kit.LocationKit'; +import { BusinessError } from '@kit.BasicServicesKit'; + +@Component +@Entry({routeName: 'MapPage'}) +struct MapPage { + @State isShowLocationPopup: boolean = false + appName: string = '' + @State isLocationPermission: boolean = false; + @State locText: string = '' + + checkLocationPermission() { + let manager = abilityAccessCtrl.createAtManager() // 得到manager对象 + let appInfo = + bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION) // 获取应用信息 + this.appName = appInfo.name; + let context: Context = getContext(this) as common.UIAbilityContext; + let permission:string = 'ohos.permission.LOCATION' + let status = manager.checkAccessTokenSync(appInfo.appInfo.accessTokenId, "ohos.permission.LOCATION") + if (status === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) { + this.isShowLocationPopup = true; + manager.requestPermissionsFromUser(context, ['ohos.permission.LOCATION'], (err: BusinessError, data: PermissionRequestResult) => { + + }); + } else { + this.getLocationAddress(); + } + } + + requestionLocationPermission() { + + } + + @Builder + locationViewBuilder() { + Row() { + Text('开启定位权限,为您推荐附近商家与好物 ').fontSize(15); + Button('去开启') + .type(ButtonType.Normal) + .height(24) + .borderRadius(12) + .width(60) + .backgroundColor(Color.Red) + .onClick(() => { + if (this.isLocationPermission) { + this.checkLocationPermission() + } + }) + + SymbolGlyph($r('sys.symbol.xmark')).width(24).height(24).borderRadius(12).onClick(() => { + this.isShowLocationPopup = false; + }) + } + .height(40) + .alignItems(VerticalAlign.Center) + .padding(5) + .position({ x: 7, y: 50 }) + .zIndex(999) + .backgroundColor('#123333') + .borderRadius(10) + .opacity(0.7) + } + + build() { + + Column(){ + if (this.isShowLocationPopup) { + this.locationViewBuilder(); + } + Text(this.locText); + }.width('100%').height('100%') + } + + aboutToAppear(): void { + this.checkLocationPermission() + } + + getLocationAddress() { + let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude": 31.12, "longitude": 121.11, "maxItems": 1}; + try { + geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => { + if (err) { + console.error('getAddressesFromLocation: err=' + JSON.stringify(err)); + } + if (data) { + console.log('getAddressesFromLocation: data=' + JSON.stringify(data)); + this.locText = data[0].placeName ?? ''; + } + }); + } catch (err) { + console.error("errCode:" + JSON.stringify(err)); + } + } +} \ No newline at end of file diff --git a/app/src/main/module.json5 b/app/src/main/module.json5 index 8c0345a..54ef8d1 100644 --- a/app/src/main/module.json5 +++ b/app/src/main/module.json5 @@ -10,6 +10,12 @@ "tablet", "2in1" ], + "metadata": [ + { + "name": "client_id", + "value": "123456789" + } + ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", @@ -37,7 +43,18 @@ ], "requestPermissions": [ { - "name" : "ohos.permission.INTERNET" + "name": "ohos.permission.LOCATION", + "reason": "$string:location_permission", + "usedScene": { + "when": "inuse" + } + }, + { + "name": "ohos.permission.APPROXIMATELY_LOCATION", + "reason": "$string:approximately_location", + "usedScene": { + "when": "inuse" + } } ] } diff --git a/app/src/main/resources/base/element/string.json b/app/src/main/resources/base/element/string.json index 24874f9..fdac89b 100644 --- a/app/src/main/resources/base/element/string.json +++ b/app/src/main/resources/base/element/string.json @@ -15,6 +15,14 @@ { "name": "set_language_title", "value": "Set Language" + }, + { + "name": "location_permission", + "value": "location_permission" + }, + { + "name": "approximately_location", + "value": "approximately_location" } ] } \ No newline at end of file diff --git a/app/src/main/resources/base/profile/main_pages.json b/app/src/main/resources/base/profile/main_pages.json index dec409a..dbb7b1e 100644 --- a/app/src/main/resources/base/profile/main_pages.json +++ b/app/src/main/resources/base/profile/main_pages.json @@ -27,6 +27,10 @@ "pages/system/SetLanguagePage", "pages/web/WebPage", - "pages/web/WebDialogPage" + "pages/web/WebDialogPage", + + "pages/map/MapPage" + + ] } diff --git a/app/src/main/resources/en_US/element/string.json b/app/src/main/resources/en_US/element/string.json index b507587..cddbd73 100644 --- a/app/src/main/resources/en_US/element/string.json +++ b/app/src/main/resources/en_US/element/string.json @@ -15,6 +15,14 @@ { "name": "set_language_title", "value": "Set Language" + }, + { + "name": "location_permission", + "value": "location_permission" + }, + { + "name": "approximately_location", + "value": "approximately_location" } ] } \ No newline at end of file diff --git a/app/src/main/resources/zh_CN/element/string.json b/app/src/main/resources/zh_CN/element/string.json index 1f661c2..ce38bc0 100644 --- a/app/src/main/resources/zh_CN/element/string.json +++ b/app/src/main/resources/zh_CN/element/string.json @@ -15,6 +15,14 @@ { "name": "set_language_title", "value": "设置语言" + }, + { + "name": "location_permission", + "value": "location_permission" + }, + { + "name": "approximately_location", + "value": "approximately_location" } ] } \ No newline at end of file diff --git a/common_ui/src/main/ets/utils/Res.ets b/common_ui/src/main/ets/utils/Res.ets index e3a6806..ee7046f 100644 --- a/common_ui/src/main/ets/utils/Res.ets +++ b/common_ui/src/main/ets/utils/Res.ets @@ -15,4 +15,12 @@ export class Res { let retStr = textDecoder.decodeWithStream( content , decodeWithStreamOptions); return retStr; } + + static getResStr(descName: string, ...rest: string[]): ResourceStr { + try { + return $r(`app.string.${descName}`, ...rest); + } catch (e) { + return ''; + } + } } \ No newline at end of file