1# Using LocationButton
2
3The **LocationButton** component comes with an intuitive and easy-to-understand identifier to let the user clearly know that it is a button for obtaining location information. This meets the requirement that the authorization scenario should match the real user intent. The application can obtain temporary authorization to get location information and complete the service functionality only when the user taps this button after understanding the usage of this button.
4
5Once the component integrated in an application is tapped, the application running in the foreground can obtain the precise location permission no matter whether the application has requested or been granted with this permission.
6
7You can use the **LocationButton** component in applications that do not strongly depend on location (such as navigation and health applications) and applications that require location information only in certain foreground scenarios (such as locating city, clocking in/out, and sharing the location). If location information is required for a long period of time or in the background, you are advised to apply for the location permission for your application.
8
9The following figure shows the effect of the **LocationButton** component.
10
11![](figures/LocationButton_effect.png)
12
13
14## Constraints
15
16- When a user clicks **LocationButton** in the application for the first time, a dialog box will be displayed to request user authorization. If the user clicks **Deny**, the dialog box will be closed and the application does not have the permission. When the user clicks **LocationButton** again, the user authorization dialog box will be displayed again. If the user clicks **Allow**, the dialog box will be closed and the application is granted the temporary location permission. After that, if the user clicks **LocationButton** again, no dialog box will be displayed.
17
18- The temporary authorization of the precise location permission remains valid until the screen is turned off, the application is switched to the background, or the application exits. Then, the permission state transits to the state (authorized, unauthorized, or unrequested) that it was prior to the temporary authorization.
19
20- During the authorization period, there is no limit on the number of API calls.
21
22- The **LocationButton** component must be visible and legible to users. You need to properly configure the component attributes such as the size and color to prevent authorization failures. If the authorization fails due to invalid component style, check the device error logs.
23
24
25## How to Develop
26
27If you need to share real-time location on a chat screen, the application only needs to access the location information for a short period of time in the foreground. In this case, you can use the **LocationButton** component, which eliminates the permission requesting process, complies with the principle of the least required permission, and elevates the user privacy experience.
28
29The following example implements the following: After **Current Location** is tapped, the application obtains the precise location permission, and a dialog box is displayed to provide the specific location information. See the figure above.
30
311. Import the dependency service **geoLocationManager**.
32
33   ```ts
34   import { geoLocationManager } from '@kit.LocationKit';
35   ```
36
372. Add the **LocationButton** component and obtain the current location information.
38
39   **LocationButton** is a button-like component consisting of an icon, text, and background. Either the icon or text is mandatory, and the background is mandatory. The icon and text cannot be customized. You can only select from the existing options.
40
41   When declaring the API for creating a security component, you can determine whether to pass in parameters. If parameters are passed in, the component is created based on the specified parameters. If no parameter is passed in, a component with default icon, text, and background is created.
42
43   The following example uses default parameters. For details, see [LocationButton](../../reference/apis-arkui/arkui-ts/ts-security-components-locationbutton.md). In addition, all security components inherit the [Security Component Universal Attributes](../../reference/apis-arkui/arkui-ts/ts-securitycomponent-attributes.md), which can be used to customize styles.
44
45   Use [geoLocationManager](../../reference/apis-location-kit/js-apis-geoLocationManager.md) in the **onClick()** callback of the **LocationButton** component to obtain the current location information.
46
47   ```ts
48   import { geoLocationManager } from '@kit.LocationKit';
49   import { promptAction } from '@kit.ArkUI';
50   import { BusinessError } from '@kit.BasicServicesKit';
51
52   // Obtain the current location information.
53   function getCurrentLocationInfo() {
54     const requestInfo: geoLocationManager.LocationRequest = {
55       'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
56       'scenario': geoLocationManager.LocationRequestScenario.UNSET,
57       'timeInterval': 1,
58       'distanceInterval': 0,
59       'maxAccuracy': 0
60     };
61     try {
62       geoLocationManager.getCurrentLocation(requestInfo)
63         .then((location: geoLocationManager.Location) => {
64           promptAction.showToast({ message: JSON.stringify(location) });
65         })
66         .catch((err: BusinessError) => {
67           console.error(`Failed to get current location. Code is ${err.code}, message is ${err.message}`);
68         });
69     } catch (err) {
70       console.error(`Failed to get current location. Code is ${err.code}, message is ${err.message}`);
71     }
72   }
73
74   @Entry
75   @Component
76   struct Index {
77     build() {
78       Row() {
79         Column({ space: 10 }) {
80           LocationButton({
81             icon: LocationIconStyle.LINES,
82             text: LocationDescription.CURRENT_LOCATION,
83             buttonType: ButtonType.Normal
84           })
85             .padding({top: 12, bottom: 12, left: 24, right: 24})
86             .onClick((event: ClickEvent, result: LocationButtonOnClickResult) => {
87               if (result === LocationButtonOnClickResult.SUCCESS) {
88                 // Obtain temporary authorization to get the location information without requesting the location permission.
89                 getCurrentLocationInfo();
90               } else {
91                 promptAction.showToast({message: 'Failed to obtain the location information.'})
92               }
93             })
94         }
95         .width('100%')
96       }
97       .height('100%')
98       .backgroundColor(0xF1F3F5)
99     }
100   }
101   ```
102