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