1# 拉起导航类应用(startAbilityByType) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: AGC--> 5<!--Owner: @liusu23--> 6<!--Designer: @xukeke--> 7<!--Tester: @lusq--> 8<!--Adviser: @huipeizi--> 9 10本章节介绍如何拉起导航类应用扩展面板。 11 12## 导航类应用扩展面板参数说明 13 14startAbilityByType接口中type字段为navigation,支持路线规划、导航、位置搜索三种意图场景,对应的wantParam参数如下: 15 16> **说明:** 17> 本文中的经纬度均采用GCJ-02坐标系统。 18 19- 路线规划场景 20 21 | 参数名 | 类型 | 必填 | 说明 | 22 | -------------------- | ---------------------- | ---- | ---------------------------------------------------- | 23 | sceneType | number | 否 | 意图场景,表明本次请求对应的操作意图。默认为1,路线规划场景填1或不填。 | 24 | originName | string | 否 | 起点名称。 | 25 | originLatitude | number | 否 | 起点纬度。 | 26 | originLongitude | number | 否 | 起点经度。 | 27 | originPoiIds | Record<number, string> | 否 | 起点POI ID列表,当前仅支持传入花瓣地图、高德地图、百度地图的POI ID。| 28 | destinationName | string | 否 | 终点名称。 | 29 | destinationLatitude | number | 是 | 终点纬度。 | 30 | destinationLongitude | number | 是 | 终点经度。 | 31 | destinationPoiIds | Record<number, string> | 否 | 终点POI ID列表,当前仅支持传入花瓣地图、高德地图、百度地图的POI ID。| 32 | vehicleType | number | 否 | 交通出行工具,取值:0-驾车,1-步行,2-骑行,3-公交。 | 33 34- 导航场景 35 36 | 参数名 | 类型 | 必填 | 说明 | 37 | -------------------- | ---------------------- | ---- | ----------------- | 38 | sceneType | number | 是 | 意图场景,表明本次请求对应的操作意图。导航场景填2。 | 39 | destinationName | string | 否 | 终点名称。 | 40 | destinationLatitude | number | 是 | 终点纬度。 | 41 | destinationLongitude | number | 是 | 终点经度。 | 42 | destinationPoiIds | Record<number, string> | 否 | 终点POI ID列表,当前仅支持传入花瓣地图、高德地图、百度地图的POI ID。| 43 44- 位置搜索场景 45 46 | 参数名 | 类型 | 必填 | 说明 | 47 | --------------- | ------ | ---- | --------------------- | 48 | sceneType | number | 是 | 意图场景,表明本次请求对应的操作意图。位置搜索场景填3。 | 49 | destinationName | string | 是 | 地点名称。 | 50 51 52## 拉起方开发步骤 53 541. 导入相关模块。 55 ```ts 56 import { common } from '@kit.AbilityKit'; 57 ``` 582. 构造接口参数并调用startAbilityByType接口。 59 60 终点POI ID列表(destinationPoiIds)和起点POI ID列表(originPoiIds)需开发者自行从各地图系统中获取,并按照对应关系传参。 61 62 63 ```ts 64 @Entry 65 @Component 66 struct Index { 67 @State hideAbility: string = 'hideAbility' 68 69 build() { 70 Row() { 71 Column() { 72 Text(this.hideAbility) 73 .fontSize(30) 74 .fontWeight(FontWeight.Bold) 75 .onClick(() => { 76 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 77 let wantParam: Record<string, Object> = { 78 'sceneType': 1, 79 'destinationLatitude': 32.060844, 80 'destinationLongitude': 118.78315, 81 'destinationName': 'xx市xx路xx号', 82 'destinationPoiIds': { 83 1: '1001', // key为1代表花瓣地图,value需为花瓣地图POI 84 2: '2002', // key为2代表高德地图,value需为高德地图POI 85 3: '3003' // key为3代表百度地图,value需为百度地图POI 86 } as Record<number, string>, 87 'originName': 'xx市xx公园', 88 'originLatitude': 31.060844, 89 'originLongitude': 120.78315, 90 'originPoiIds': { 91 1: '1101', // key为1代表花瓣地图,value需为花瓣地图POI 92 2: '2202', // key为2代表高德地图,value需为高德地图POI 93 3: '3303' // key为3代表百度地图,value需为百度地图POI 94 } as Record<number, string>, 95 'vehicleType': 0 96 }; 97 let abilityStartCallback: common.AbilityStartCallback = { 98 onError: (code: number, name: string, message: string) => { 99 console.error(`onError code ${code} name: ${name} message: ${message}`); 100 }, 101 onResult: (result) => { 102 console.info(`onResult result: ${JSON.stringify(result)}`); 103 } 104 } 105 106 context.startAbilityByType("navigation", wantParam, abilityStartCallback, 107 (err) => { 108 if (err) { 109 console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); 110 } else { 111 console.info(`success`); 112 } 113 }); 114 }); 115 } 116 .width('100%') 117 } 118 .height('100%') 119 } 120 } 121 ``` 122 效果示例图: 123 124  125 126## 目标方开发步骤 127 1281. 在module.json5中配置[uris](../quick-start/module-configuration-file.md#skills标签),步骤如下: 129 1. 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下: 130 | 取值 | 含义 | 131 | -------------- | ---------------------------- | 132 | Navigation | 声明应用支持导航功能 | 133 | RoutePlan | 声明应用支持路线规划功能 | 134 | PlaceSearch | 声明应用支持位置搜索功能 | 135 2. 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。 136 ```json 137 { 138 "abilities": [ 139 { 140 "skills": [ 141 { 142 "uris": [ 143 { 144 "scheme": "maps", // 这里仅示意,应用需确保这里声明的uri能被外部正常拉起 145 "host": "navigation", 146 "path": "", 147 "linkFeature": "Navigation" // 声明应用支持导航功能 148 }, 149 { 150 "scheme": "maps", // 这里仅示意,应用需确保这里声明的uri能被外部正常拉起 151 "host": "routePlan", 152 "path": "", 153 "linkFeature": "RoutePlan" // 声明应用支持路线规划功能 154 }, 155 { 156 "scheme": "maps", // 这里仅示意,应用需确保这里声明的uri能被外部正常拉起 157 "host": "search", 158 "path": "", 159 "linkFeature": "PlaceSearch" // 声明应用支持位置搜索功能 160 } 161 ] 162 } 163 ] 164 } 165 ] 166 } 167 ``` 168 1692. 解析参数并做对应处理。 170 171 ```ts 172 UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void 173 ``` 174 175 在参数**want.uri**中会携带目标方配置的linkFeature对应的uri。 176 177 在参数**want.parameters**中会携带Caller方传入的参数,不同场景参数如下所示。 178 179 - 路线规划场景 180 181 | 参数名 | 类型 | 必填 | 说明 | 182 | -------------------- | ------ | ---- | ---------------------------------------------------- | 183 | originName | string | 否 | 起点名称。 | 184 | originLatitude | number | 否 | 起点纬度。 | 185 | originLongitude | number | 否 | 起点经度。 | 186 | originPoiId | string | 否 | 起点POI ID,当前仅支持花瓣地图、高德地图、百度地图获取此参数。 | 187 | destinationName | string | 否 | 终点名称。 | 188 | destinationLatitude | number | 是 | 终点纬度。 | 189 | destinationLongitude | number | 是 | 终点经度。 | 190 | destinationPoiId | string | 否 | 终点POI ID,当前仅支持花瓣地图、高德地图、百度地图获取此参数。 | 191 | vehicleType | number | 否 | 交通出行工具,取值:0-驾车,1-步行,2-骑行,3-公交。 | 192 193 - 导航场景 194 195 | 参数名 | 类型 | 必填 | 说明 | 196 | -------------------- | ------ | ---- | ---------- | 197 | destinationName | string | 否 | 终点名称。 | 198 | destinationLatitude | number | 是 | 终点纬度。 | 199 | destinationLongitude | number | 是 | 终点经度。 | 200 | destinationPoiId | string | 否 | 终点POI ID,当前仅支持花瓣地图、高德地图、百度地图获取此参数。| 201 202 - 位置搜索场景 203 204 | 参数名 | 类型 | 必填 | 说明 | 205 | --------------- | ------ | ---- | -------- | 206 | destinationName | string | 是 | 地点名称。 | 207 208 应用可根据[linkFeature](../quick-start/module-configuration-file.md#skills标签)中定义的特性功能,比如路线规划、导航和位置搜索,结合接收到的uri和参数开发不同的样式页面。 209 210**完整示例:** 211 212```ts 213import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 214import { hilog } from '@kit.PerformanceAnalysisKit'; 215import { window } from '@kit.ArkUI'; 216 217const TAG = 'EntryAbility'; 218 219export default class EntryAbility extends UIAbility { 220 windowStage: window.WindowStage | null = null; 221 222 uri?: string; 223 destinationLatitude?: number; 224 destinationLongitude?: number; 225 destinationName?: string; 226 originName?: string; 227 originLatitude?: number; 228 originLongitude?: number; 229 vehicleType?: number; 230 destinationPoiId?: string; 231 originPoiId?: string; 232 233 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 234 hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`); 235 super.onCreate(want, launchParam); 236 this.parseWant(want); 237 } 238 239 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 240 hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`); 241 super.onNewWant(want, launchParam); 242 this.parseWant(want); 243 if (!this.windowStage) { 244 hilog.error(0x0000, TAG, 'windowStage is null'); 245 this.context.terminateSelf(); 246 return; 247 } 248 this.loadPage(this.windowStage); 249 } 250 251 private parseWant(want: Want): void { 252 this.uri = want.uri as string | undefined; 253 this.destinationLatitude = want.parameters?.destinationLatitude as number | undefined; 254 this.destinationLongitude = want.parameters?.destinationLongitude as number | undefined; 255 this.destinationName = want.parameters?.destinationName as string | undefined; 256 this.originName = want.parameters?.originName as string | undefined; 257 this.originLatitude = want.parameters?.originLatitude as number | undefined; 258 this.originLongitude = want.parameters?.originLongitude as number | undefined; 259 this.vehicleType = want.parameters?.vehicleType as number | undefined; 260 this.destinationPoiId = want.parameters?.destinationPoiId as string | undefined; 261 this.originPoiId = want.parameters?.originPoiId as string | undefined; 262 } 263 264 private loadPage(windowStage: window.WindowStage): void { 265 hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`); 266 if (this.uri === 'maps://navigation') { 267 // 构建导航场景参数 268 const storage: LocalStorage = new LocalStorage({ 269 "destinationLatitude": this.destinationLatitude, 270 "destinationLongitude": this.destinationLongitude, 271 "destinationPoiId": this.destinationPoiId 272 } as Record<string, Object>); 273 // 拉起导航页面 274 windowStage.loadContent('pages/NavigationPage', storage) 275 } else if (this.uri === 'maps://routePlan') { 276 // 构建路径规划场景参数 277 const storage: LocalStorage = new LocalStorage({ 278 "destinationLatitude": this.destinationLatitude, 279 "destinationLongitude": this.destinationLongitude, 280 "destinationName": this.destinationName, 281 "originName": this.originName, 282 "originLatitude": this.originLatitude, 283 "originLongitude": this.originLongitude, 284 "vehicleType": this.vehicleType, 285 "destinationPoiId": this.destinationPoiId, 286 "originPoiId": this.originPoiId 287 } as Record<string, Object>); 288 // 拉起路径规划页面 289 windowStage.loadContent('pages/RoutePlanPage', storage) 290 } else if (this.uri === 'maps://search') { 291 // 构建位置搜索场景参数 292 const storage: LocalStorage = new LocalStorage({ 293 "destinationName": this.destinationName 294 } as Record<string, Object>); 295 // 拉起位置搜索页面 296 windowStage.loadContent('pages/PlaceSearchPage', storage) 297 } else { 298 // 默认拉起首页 299 windowStage.loadContent('pages/Index', (err) => { 300 if (err.code) { 301 hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', 302 JSON.stringify(err) ?? ''); 303 return; 304 } 305 hilog.info(0x0000, TAG, 'Succeeded in loading the content.'); 306 }); 307 } 308 } 309 310 onDestroy(): void { 311 hilog.info(0x0000, TAG, `onDestroy`); 312 } 313 314 onWindowStageCreate(windowStage: window.WindowStage): void { 315 hilog.info(0x0000, TAG, `onWindowStageCreate`); 316 this.windowStage = windowStage; 317 this.loadPage(this.windowStage); 318 } 319 320 onWindowStageDestroy(): void { 321 hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); 322 } 323 324 onForeground(): void { 325 hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); 326 } 327 328 onBackground(): void { 329 hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); 330 } 331} 332```