add location

This commit is contained in:
DevWiki 2024-09-05 20:34:48 +08:00
parent ff8c5714d9
commit bdfab3ab29
11 changed files with 171 additions and 17 deletions

View File

@ -2,7 +2,7 @@
"string": [
{
"name": "app_name",
"value": "HM4Demo"
"value": "HMDemo"
}
]
}

View File

@ -1,5 +1,5 @@
import { Context } from '@ohos.arkui.UIContext';
import window from '@ohos.window';
import { window } from '@kit.ArkUI';
export class MyApp {

View File

@ -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

View File

@ -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'}
]
}
];

View File

@ -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));
}
}
}

View File

@ -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"
}
}
]
}

View File

@ -15,6 +15,14 @@
{
"name": "set_language_title",
"value": "Set Language"
},
{
"name": "location_permission",
"value": "location_permission"
},
{
"name": "approximately_location",
"value": "approximately_location"
}
]
}

View File

@ -27,6 +27,10 @@
"pages/system/SetLanguagePage",
"pages/web/WebPage",
"pages/web/WebDialogPage"
"pages/web/WebDialogPage",
"pages/map/MapPage"
]
}

View File

@ -15,6 +15,14 @@
{
"name": "set_language_title",
"value": "Set Language"
},
{
"name": "location_permission",
"value": "location_permission"
},
{
"name": "approximately_location",
"value": "approximately_location"
}
]
}

View File

@ -15,6 +15,14 @@
{
"name": "set_language_title",
"value": "设置语言"
},
{
"name": "location_permission",
"value": "location_permission"
},
{
"name": "approximately_location",
"value": "approximately_location"
}
]
}

View File

@ -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 '';
}
}
}