# 拉起导航类应用(startAbilityByType) æœ¬ç« èŠ‚ä»‹ç»å¦‚ä½•æ‹‰èµ·å¯¼èˆªç±»åº”ç”¨æ‰©å±•é¢æ¿ã€‚ ## å¯¼èˆªç±»åº”ç”¨æ‰©å±•é¢æ¿å‚数说明 startAbilityByType接å£ä¸typeå—æ®µä¸ºnavigation,支æŒè·¯çº¿è§„划ã€å¯¼èˆªã€ä½ç½®æœç´¢ä¸‰ç§æ„图场景,对应的wantParam傿•°å¦‚下: > **说明:** > 本文ä¸çš„ç»çº¬åº¦å‡é‡‡ç”¨GCJ-02åæ ‡ç³»ç»Ÿã€‚ - 路线规划场景 | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------------------- | ---------------------- | ---- | ---------------------------------------------------- | | sceneType | number | å¦ | æ„图场景,表明本次请求对应的æ“作æ„图。默认为1,路线规划场景填1或ä¸å¡« | | originName | string | å¦ | 起点åç§° | | originLatitude | number | å¦ | 起点纬度 | | originLongitude | number | å¦ | 起点ç»åº¦ | | originPoiIds | Record<number, string> | å¦ | 起点POI ID列表,当å‰ä»…支æŒä¼ 入花瓣地图和高德地图的POI ID| | destinationName | string | å¦ | 终点åç§° | | destinationLatitude | number | 是 | 终点纬度 | | destinationLongitude | number | 是 | 终点ç»åº¦ | | destinationPoiIds | Record<number, string> | å¦ | 终点POI ID列表,当å‰ä»…支æŒä¼ 入花瓣地图和高德地图的POI ID| | vehicleType | number | å¦ | 交通出行工具,å–值:0-驾车,1-æ¥è¡Œï¼Œ2-骑行,3-公交; | - 导航场景 | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------------------- | ---------------------- | ---- | ----------------- | | sceneType | number | 是 | æ„图场景,表明本次请求对应的æ“作æ„图。导航场景填2 | | destinationName | string | å¦ | 终点åç§° | | destinationLatitude | number | 是 | 终点纬度 | | destinationLongitude | number | 是 | 终点ç»åº¦ | | destinationPoiIds | Record<number, string> | å¦ | 终点POI ID列表,当å‰ä»…支æŒä¼ 入花瓣地图和高德地图的POI ID| - ä½ç½®æœç´¢åœºæ™¯ | 傿•°å | 类型 | å¿…å¡« | 说明 | | --------------- | ------ | ---- | --------------------- | | sceneType | number | 是 | æ„图场景,表明本次请求对应的æ“作æ„图。ä½ç½®æœç´¢åœºæ™¯å¡«3 | | destinationName | string | 是 | 地点åç§° | ## æ‹‰èµ·æ–¹å¼€å‘æ¥éª¤ 1. 导入相关模å—。 ```ts import { common } from '@kit.AbilityKit'; ``` 2. æž„é€ æŽ¥å£å‚数并调用startAbilityByType接å£ã€‚ 终点POI ID列表(destinationPoiIds)和起点POI ID列表(originPoiIds)需开å‘者自行从å„地图系统ä¸èŽ·å–ï¼Œå¹¶æŒ‰ç…§å¯¹åº”å…³ç³»ä¼ å‚。 ```ts let context = getContext(this) as common.UIAbilityContext; let wantParam: Record<string, Object> = { 'sceneType': 1, 'destinationLatitude': 32.060844, 'destinationLongitude': 118.78315, 'destinationName': 'xx市xxè·¯xxå·', 'destinationPoiIds': { 1: '1111', // key为1代表花瓣地图,value需为花瓣地图POI 2: '2222' // key为2代表高德地图,value需为高德地图POI } as Record<number, string>, 'originName': 'xx市xxå…¬å›', 'originLatitude': 31.060844, 'originLongitude': 120.78315, 'originPoiIds': { 1: '3333', // key为1代表花瓣地图,value需为花瓣地图POI 2: '4444' // key为2代表高德地图,value需为高德地图POI } as Record<number, string>, 'vehicleType': 0 }; let abilityStartCallback: common.AbilityStartCallback = { onError: (code: number, name: string, message: string) => { console.log(`onError code ${code} name: ${name} message: ${message}`); }, onResult: (result)=>{ console.log(`onResult result: ${JSON.stringify(result)}`); } } context.startAbilityByType("navigation", wantParam, abilityStartCallback, (err) => { if (err) { console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); } else { console.log(`success`); } }); ``` 效果示例图:  ## ç›®æ ‡æ–¹å¼€å‘æ¥éª¤ 1. 在module.json5ä¸é…ç½®[uris](../quick-start/module-configuration-file.md#skillsæ ‡ç¾),æ¥éª¤å¦‚下: 1. 设置linkFeature属性以声明当å‰åº”用支æŒçš„特性功能,从而系统å¯ä»¥ä»Žè®¾å¤‡å·²å®‰è£…åº”ç”¨ä¸æ‰¾åˆ°å½“剿”¯æŒè¯¥ç‰¹æ€§çš„应用,å–值范围如下: | å–值 | å«ä¹‰ | | -------------- | ---------------------------- | | Navigation | 声明应用支æŒå¯¼èˆªåŠŸèƒ½ | | RoutePlan | 声明应用支æŒè·¯çº¿è§„划功能 | | PlaceSearch | 声明应用支æŒä½ç½®æœç´¢åŠŸèƒ½ | 2. 设置schemeã€hostã€portã€path/pathStartWith属性,与Wantä¸URI相匹é…,以便区分ä¸åŒåŠŸèƒ½ã€‚ ```json { "abilities": [ { "skills": [ { "uris": [ { "scheme": "maps", // 这里仅示æ„,应用需确ä¿è¿™é‡Œå£°æ˜Žçš„çš„uri能被外部æ£å¸¸æ‹‰èµ· "host": "navigation", "path": "", "linkFeature": "Navigation" // 声明应用支æŒå¯¼èˆªåŠŸèƒ½ }, { "scheme": "maps", // 这里仅示æ„,应用需确ä¿è¿™é‡Œå£°æ˜Žçš„çš„uri能被外部æ£å¸¸æ‹‰èµ· "host": "routePlan", "path": "", "linkFeature": "RoutePlan" // 声明应用支æŒè·¯çº¿è§„划功能 }, { "scheme": "maps", // 这里仅示æ„,应用需确ä¿è¿™é‡Œå£°æ˜Žçš„çš„uri能被外部æ£å¸¸æ‹‰èµ· "host": "search", "path": "", "linkFeature": "PlaceSearch" // 声明应用支æŒä½ç½®æœç´¢åŠŸèƒ½ } ] } ] } ] } ``` 2. è§£æžå‚æ•°å¹¶åšå¯¹åº”处ç†ã€‚ ```ts UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void ``` åœ¨å‚æ•°**want.uri**ä¸ä¼šæºå¸¦ç›®æ ‡æ–¹é…置的linkFeature对应的uri。 åœ¨å‚æ•°**want.parameters**ä¸ä¼šæºå¸¦Calleræ–¹ä¼ å…¥çš„å‚æ•°ï¼Œä¸åŒåœºæ™¯å‚数如下所示。 - 路线规划场景 | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------------------- | ------ | ---- | ---------------------------------------------------- | | originName | string | å¦ | 起点åç§° | | originLatitude | number | å¦ | 起点纬度 | | originLongitude | number | å¦ | 起点ç»åº¦ | | originPoiId | string | å¦ | 起点POI ID,当å‰ä»…支æŒèŠ±ç“£åœ°å›¾å’Œé«˜å¾·åœ°å›¾èŽ·å–æ¤å‚æ•° | | destinationName | string | å¦ | 终点åç§° | | destinationLatitude | number | 是 | 终点纬度 | | destinationLongitude | number | 是 | 终点ç»åº¦ | | destinationPoiId | string | å¦ | 终点POI ID,当å‰ä»…支æŒèŠ±ç“£åœ°å›¾å’Œé«˜å¾·åœ°å›¾èŽ·å–æ¤å‚æ•° | | vehicleType | number | å¦ | 交通出行工具,å–值:0-驾车,1-æ¥è¡Œï¼Œ2-骑行,3-公交; | - 导航场景 | 傿•°å | 类型 | å¿…å¡« | 说明 | | -------------------- | ------ | ---- | ---------- | | destinationName | string | å¦ | 终点åç§° | | destinationLatitude | number | 是 | 终点纬度 | | destinationLongitude | number | 是 | 终点ç»åº¦ | | destinationPoiId | string | å¦ | 终点POI ID,当å‰ä»…支æŒèŠ±ç“£åœ°å›¾å’Œé«˜å¾·åœ°å›¾èŽ·å–æ¤å‚æ•°| - ä½ç½®æœç´¢åœºæ™¯ | 傿•°å | 类型 | å¿…å¡« | 说明 | | --------------- | ------ | ---- | -------- | | destinationName | string | 是 | 地点åç§° | åº”ç”¨å¯æ ¹æ®[linkFeature](../quick-start/module-configuration-file.md#skillsæ ‡ç¾)ä¸å®šä¹‰çš„特性功能,比如路线规划ã€å¯¼èˆªå’Œä½ç½®æœç´¢ï¼Œç»“åˆæŽ¥æ”¶åˆ°çš„uriå’Œå‚æ•°å¼€å‘ä¸åŒçš„æ ·å¼é¡µé¢ã€‚ **完整示例:** ```ts import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; const TAG = 'EntryAbility' export default class EntryAbility extends UIAbility { windowStage: window.WindowStage | null = null; uri?: string; destinationLatitude?: number; destinationLongitude?: number; destinationName?: string; originName?: string; originLatitude?: number; originLongitude?: number; vehicleType?: number; destinationPoiId?: string; originPoiId?: string; onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`); super.onCreate(want, launchParam); this.parseWant(want); } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`); super.onNewWant(want, launchParam); this.parseWant(want); if (!this.windowStage) { hilog.error(0x0000, TAG, 'windowStage is null'); this.context.terminateSelf(); return; } this.loadPage(this.windowStage); } private parseWant(want: Want): void { this.uri = want.uri as string | undefined; this.destinationLatitude = want.parameters?.destinationLatitude as number | undefined; this.destinationLongitude = want.parameters?.destinationLongitude as number | undefined; this.destinationName = want.parameters?.destinationName as string | undefined; this.originName = want.parameters?.originName as string | undefined; this.originLatitude = want.parameters?.originLatitude as number | undefined; this.originLongitude = want.parameters?.originLongitude as number | undefined; this.vehicleType = want.parameters?.vehicleType as number | undefined; this.destinationPoiId = want.parameters?.destinationPoiId as string | undefined; this.originPoiId = want.parameters?.originPoiId as string | undefined; } private loadPage(windowStage: window.WindowStage): void { hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`); if (this.uri === 'maps://navigation') { // æž„å»ºå¯¼èˆªåœºæ™¯å‚æ•° const storage: LocalStorage = new LocalStorage({ "destinationLatitude": this.destinationLatitude, "destinationLongitude": this.destinationLongitude, "destinationPoiId": this.destinationPoiId } as Record<string, Object>); // æ‹‰èµ·å¯¼èˆªé¡µé¢ windowStage.loadContent('pages/NavigationPage', storage) } else if (this.uri === 'maps://routePlan') { // æž„å»ºè·¯å¾„è§„åˆ’åœºæ™¯å‚æ•° const storage: LocalStorage = new LocalStorage({ "destinationLatitude": this.destinationLatitude, "destinationLongitude": this.destinationLongitude, "destinationName": this.destinationName, "originName": this.originName, "originLatitude": this.originLatitude, "originLongitude": this.originLongitude, "vehicleType": this.vehicleType, "destinationPoiId": this.destinationPoiId, "originPoiId": this.originPoiId } as Record<string, Object>); // æ‹‰èµ·è·¯å¾„è§„åˆ’é¡µé¢ windowStage.loadContent('pages/RoutePlanPage', storage) } else if (this.uri === 'maps://search') { // 构建ä½ç½®æœç´¢åœºæ™¯å‚æ•° const storage: LocalStorage = new LocalStorage({ "destinationName": this.destinationName } as Record<string, Object>); // 拉起ä½ç½®æœç´¢é¡µé¢ windowStage.loadContent('pages/PlaceSearchPage', storage) } else { // 默认拉起首页 windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, TAG, 'Succeeded in loading the content.'); }); } } onDestroy(): void { hilog.info(0x0000, TAG, `onDestroy`); } onWindowStageCreate(windowStage: window.WindowStage): void { hilog.info(0x0000, TAG, `onWindowStageCreate`); this.windowStage = windowStage; this.loadPage(this.windowStage); } onWindowStageDestroy(): void { hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); } onBackground(): void { hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); } } ```