• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using startAbilityByType to Start a Navigation Application
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: AGC-->
5<!--Owner: @liusu23-->
6<!--Designer: @xukeke-->
7<!--Tester: @yizhixiaosiqin-->
8<!--Adviser: @huipeizi-->
9
10This topic describes how to open the vertical domain panel of navigation applications.
11
12## Parameters on the Navigation Application Panel
13
14If the **type** field in **startAbilityByType** is set to **navigation**, three intent scenarios are supported: route planning, navigation, and place search. The corresponding **wantParam** parameter contains the following properties.
15
16> **NOTE**
17>
18> The GCJ-02 coordinate system is used for the longitude and latitude in this document.
19
20- Route planning scenarios
21
22    | Name              | Type                  | Mandatory| Description                                                |
23    | -------------------- | ---------------------- | ---- | ---------------------------------------------------- |
24    | sceneType            | number                 | No  | Intent scene, which indicates the purpose of the current request. The default value is **1**. In route planning scenarios, set it to **1** or leave it empty.                  |
25    | originName           | string                 | No  | Name of the source.                                            |
26    | originLatitude       | number                 | No  | Latitude of the source.                                            |
27    | originLongitude      | number                 | No  | Longitude of the source.                                            |
28    | originPoiIds         | Record<number, string> | No  | List of POI IDs of the source. Currently, only POI IDs of Petal Maps, AutoNavi Maps, and Baidu Maps can be passed.|
29    | destinationName      | string                 | No  | Name of the destination.                                            |
30    | destinationLatitude  | number                 | Yes  | Latitude of the destination.                                            |
31    | destinationLongitude | number                 | Yes  | Longitude of the destination.                                            |
32    | destinationPoiIds    | Record<number, string> | No  | List of POI IDs of the destination. Currently, only POI IDs of Petal Maps, AutoNavi Maps, and Baidu Maps can be passed.|
33    | vehicleType          | number                 | No  | Transportation mode. The options are as follows: 0: driving; 1: walking; 2: cycling; 3: public transportation.|
34
35- Navigation scenarios
36
37    | Name              | Type                  | Mandatory| Description             |
38    | -------------------- | ---------------------- | ---- | ----------------- |
39    | sceneType            | number                 | Yes  | Intent scene, which indicates the purpose of the current request. Set it to **2** for navigation scenarios.|
40    | destinationName      | string                 | No  | Name of the destination.         |
41    | destinationLatitude  | number                 | Yes  | Latitude of the destination.         |
42    | destinationLongitude | number                 | Yes  | Longitude of the destination.         |
43    | destinationPoiIds    | Record<number, string> | No  | List of POI IDs of the destination. Currently, only POI IDs of Petal Maps, AutoNavi Maps, and Baidu Maps can be passed.|
44
45- Place search scenarios
46
47    | Name         | Type  | Mandatory| Description                 |
48    | --------------- | ------ | ---- | --------------------- |
49    | sceneType       | number | Yes  | Intent scene, which indicates the purpose of the current request. Set it to **3** for place search scenarios.|
50    | destinationName | string | Yes  | Name of the destination.             |
51
52
53## Developing a Caller Application
54
551. Import the module.
56    ```ts
57    import { common } from '@kit.AbilityKit';
58    ```
592. Construct parameters and call the **startAbilityByType** API.
60
61   You need to obtain the POI IDs of the destination and origin from each map system and pass the parameters **destinationPoiIds** and **originPoiIds** based on the mappings.
62
63
64    ```ts
65	@Entry
66	@Component
67	struct Index {
68	@State hideAbility: string = 'hideAbility'
69
70		build() {
71			Row() {
72				Column() {
73					Text(this.hideAbility)
74						.fontSize(30)
75						.fontWeight(FontWeight.Bold)
76						.onClick(() => {
77							let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
78							let wantParam: Record<string, Object> = {
79								'sceneType': 1,
80								'destinationLatitude': 32.060844,
81								'destinationLongitude': 118.78315,
82								'destinationName': 'No.xx, xx Road, xx City',
83								'destinationPoiIds': {
84									1: '1001', // Key 1 indicates Petal Maps, and the value must be a POI in Petal Maps.
85									2: '2002', // Key 2 indicates AutoNavi Map, and the value must be a POI in AutoNavi Maps.
86									3: '3003' // Key 3 indicates Baidu Maps, and the value must be a POI in Baidu Maps.
87								} as Record<number, string>,
88								'originName': 'xx Park in xx City',
89								'originLatitude': 31.060844,
90								'originLongitude': 120.78315,
91								'originPoiIds': {
92									1: '1101', // Key 1 indicates Petal Maps, and the value must be a POI in Petal Maps.
93									2: '2202', // Key 2 indicates AutoNavi Map, and the value must be a POI in AutoNavi Maps.
94									3: '3303' // Key 3 indicates Baidu Maps, and the value must be a POI in Baidu Maps.
95								} as Record<number, string>,
96								'vehicleType': 0
97							};
98							let abilityStartCallback: common.AbilityStartCallback = {
99								onError: (code: number, name: string, message: string) => {
100									console.log(`onError code ${code} name: ${name} message: ${message}`);
101								},
102								onResult: (result) => {
103									console.log(`onResult result: ${JSON.stringify(result)}`);
104								}
105							}
106
107							context.startAbilityByType("navigation", wantParam, abilityStartCallback,
108								(err) => {
109									if (err) {
110										console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
111									} else {
112										console.log(`success`);
113									}
114								});
115						});
116				}
117				.width('100%')
118			}
119			.height('100%')
120		}
121	}
122    ```
123
124**Effect**
125
126![Effect example](./figures/start-navigation-panel.png)
127
128## Developing a Target Application
129
1301. Configure [uris](../quick-start/module-configuration-file.md#skills) in the **module.json5** file.
131    1. Set the **linkFeature** field to declare the features supported by the application so that the system can match the application against all the installed applications on the device. The options are as follows:
132        | Value          | Description                        |
133        | -------------- | ---------------------------- |
134        | Navigation     | The application supports navigation.		|
135        | RoutePlan      | The application supports route planning.	|
136        | PlaceSearch    | The application supports place search.    |
137    2. Set **scheme**, **host**, **port**, and **path** or **pathStartWith** to match the URIs in Want to distinguish different features.
138    ```json
139    {
140      "abilities": [
141          {
142          "skills": [
143              {
144              "uris": [
145                  {
146                  "scheme": "maps", // It is for reference only. Ensure that the declared URI can be started by external systems.
147                  "host": "navigation",
148                  "path": "",
149                  "linkFeature": "Navigation" // Declare that the application supports navigation.
150                  },
151                  {
152                  "scheme": "maps", // It is for reference only. Ensure that the declared URI can be started by external systems.
153                  "host": "routePlan",
154                  "path": "",
155                  "linkFeature": "RoutePlan" // Declare that the application supports route planning.
156                  },
157                  {
158                  "scheme": "maps", // It is for reference only. Ensure that the declared URI can be started by external systems.
159                  "host": "search",
160                  "path": "",
161                  "linkFeature": "PlaceSearch" // Declare that the application supports place search.
162                  }
163              ]
164              }
165          ]
166          }
167      ]
168    }
169    ```
170
1712. Parse parameters and perform corresponding processing.
172
173    ```ts
174    UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
175    ```
176
177    The **want.uri** parameter carries the URI corresponding to **linkFeature** configured by the target application.
178
179    **want.parameters** carries the parameters passed by the caller, which vary in different scenarios.
180
181    - Route planning scenarios
182
183        | Name              | Type  | Mandatory| Description                                                |
184        | -------------------- | ------ | ---- | ---------------------------------------------------- |
185        | originName           | string | No  | Name of the source.                                            |
186        | originLatitude       | number | No  | Latitude of the source.                                            |
187        | originLongitude      | number | No  | Longitude of the source.                                            |
188        | originPoiId          | string | No  | POI ID of the source. Currently, this parameter can be obtained only from Petal Maps, AutoNavi Maps, and Baidu Maps.     |
189        | destinationName      | string | No  | Name of the destination.                                            |
190        | destinationLatitude  | number | Yes  | Latitude of the destination.                                            |
191        | destinationLongitude | number | Yes  | Longitude of the destination.                                            |
192        | destinationPoiId     | string | No  | POI ID of the destination. Currently, this parameter can be obtained only from Petal Maps, AutoNavi Maps, and Baidu Maps.     |
193        | vehicleType          | number | No  | Transportation mode. The options are as follows: 0: driving; 1: walking; 2: cycling; 3: public transportation.|
194
195    - Navigation scenarios
196
197        | Name              | Type  | Mandatory| Description      |
198        | -------------------- | ------ | ---- | ---------- |
199        | destinationName      | string | No  | Name of the destination.  |
200        | destinationLatitude  | number | Yes  | Latitude of the destination.  |
201        | destinationLongitude | number | Yes  | Longitude of the destination.  |
202        | destinationPoiId     | string | No  | POI ID of the destination. Currently, this parameter can be obtained only from Petal Maps, AutoNavi Maps, and Baidu Maps.|
203
204    - Place search scenarios
205
206        | Name         | Type  | Mandatory| Description    |
207        | --------------- | ------ | ---- | -------- |
208        | destinationName | string | Yes  | Name of the destination.|
209
210The application can develop different style pages based on the features defined in [linkFeature](../quick-start/module-configuration-file.md#skills), such as route planning, navigation, and place search, as well as the received URI and parameters.
211
212**Sample Code**
213
214```ts
215import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
216import { hilog } from '@kit.PerformanceAnalysisKit';
217import { window } from '@kit.ArkUI';
218
219const TAG = 'EntryAbility';
220
221export default class EntryAbility extends UIAbility {
222    windowStage: window.WindowStage | null = null;
223
224    uri?: string;
225    destinationLatitude?: number;
226    destinationLongitude?: number;
227    destinationName?: string;
228    originName?: string;
229    originLatitude?: number;
230    originLongitude?: number;
231    vehicleType?: number;
232    destinationPoiId?: string;
233    originPoiId?: string;
234
235    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
236        hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
237        super.onCreate(want, launchParam);
238        this.parseWant(want);
239    }
240
241    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
242        hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
243        super.onNewWant(want, launchParam);
244        this.parseWant(want);
245        if (!this.windowStage) {
246            hilog.error(0x0000, TAG, 'windowStage is null');
247            this.context.terminateSelf();
248            return;
249        }
250        this.loadPage(this.windowStage);
251    }
252
253    private parseWant(want: Want): void {
254        this.uri = want.uri as string | undefined;
255        this.destinationLatitude = want.parameters?.destinationLatitude as number | undefined;
256        this.destinationLongitude = want.parameters?.destinationLongitude as number | undefined;
257        this.destinationName = want.parameters?.destinationName as string | undefined;
258        this.originName = want.parameters?.originName as string | undefined;
259        this.originLatitude = want.parameters?.originLatitude as number | undefined;
260        this.originLongitude = want.parameters?.originLongitude as number | undefined;
261        this.vehicleType = want.parameters?.vehicleType as number | undefined;
262        this.destinationPoiId = want.parameters?.destinationPoiId as string | undefined;
263        this.originPoiId = want.parameters?.originPoiId as string | undefined;
264    }
265
266    private loadPage(windowStage: window.WindowStage): void {
267        hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`);
268        if (this.uri === 'maps://navigation') {
269            // Construct parameters for the navigation scenario.
270            const storage: LocalStorage = new LocalStorage({
271                "destinationLatitude": this.destinationLatitude,
272                "destinationLongitude": this.destinationLongitude,
273                "destinationPoiId": this.destinationPoiId
274            } as Record<string, Object>);
275            // Open the navigation page.
276            windowStage.loadContent('pages/NavigationPage', storage)
277        } else if (this.uri === 'maps://routePlan') {
278            // Construct parameters for the path planning scenario.
279            const storage: LocalStorage = new LocalStorage({
280                "destinationLatitude": this.destinationLatitude,
281                "destinationLongitude": this.destinationLongitude,
282                "destinationName": this.destinationName,
283                "originName": this.originName,
284                "originLatitude": this.originLatitude,
285                "originLongitude": this.originLongitude,
286                "vehicleType": this.vehicleType,
287                "destinationPoiId": this.destinationPoiId,
288                "originPoiId": this.originPoiId
289            } as Record<string, Object>);
290            // Open the route planning page.
291            windowStage.loadContent('pages/RoutePlanPage', storage)
292        }  else if (this.uri === 'maps://search') {
293            // Construct parameters for the place search scenario.
294            const storage: LocalStorage = new LocalStorage({
295                "destinationName": this.destinationName
296            } as Record<string, Object>);
297            // Open the place search page.
298            windowStage.loadContent('pages/PlaceSearchPage', storage)
299        } else {
300            // Display the home page by default.
301            windowStage.loadContent('pages/Index', (err) => {
302                if (err.code) {
303                    hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s',
304                        JSON.stringify(err) ?? '');
305                    return;
306                }
307                hilog.info(0x0000, TAG, 'Succeeded in loading the content.');
308            });
309        }
310    }
311
312    onDestroy(): void {
313        hilog.info(0x0000, TAG, `onDestroy`);
314    }
315
316    onWindowStageCreate(windowStage: window.WindowStage): void {
317        hilog.info(0x0000, TAG, `onWindowStageCreate`);
318        this.windowStage = windowStage;
319        this.loadPage(this.windowStage);
320    }
321
322    onWindowStageDestroy(): void {
323        hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy');
324    }
325
326    onForeground(): void {
327        hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground');
328    }
329
330    onBackground(): void {
331        hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground');
332    }
333}
334```
335