1e41f4b71Sopenharmony_ci# Using LocationButton 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **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. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciOnce 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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciYou 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe following figure shows the effect of the **LocationButton** component. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## Constraints 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- 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. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- 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. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci- During the authorization period, there is no limit on the number of API calls. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci- 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. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## How to Develop 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciIf 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. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe 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. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci1. Import the dependency service **geoLocationManager**. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci ```ts 34e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 35e41f4b71Sopenharmony_ci ``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci2. Add the **LocationButton** component and obtain the current location information. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci **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. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci 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. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 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. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci Use [geoLocationManager](../../reference/apis-location-kit/js-apis-geoLocationManager.md) in the **onClick()** callback of the **LocationButton** component to obtain the current location information. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```ts 48e41f4b71Sopenharmony_ci import { geoLocationManager } from '@kit.LocationKit'; 49e41f4b71Sopenharmony_ci import { promptAction } from '@kit.ArkUI'; 50e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci // Obtain the current location information. 53e41f4b71Sopenharmony_ci function getCurrentLocationInfo() { 54e41f4b71Sopenharmony_ci const requestInfo: geoLocationManager.LocationRequest = { 55e41f4b71Sopenharmony_ci 'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, 56e41f4b71Sopenharmony_ci 'scenario': geoLocationManager.LocationRequestScenario.UNSET, 57e41f4b71Sopenharmony_ci 'timeInterval': 1, 58e41f4b71Sopenharmony_ci 'distanceInterval': 0, 59e41f4b71Sopenharmony_ci 'maxAccuracy': 0 60e41f4b71Sopenharmony_ci }; 61e41f4b71Sopenharmony_ci try { 62e41f4b71Sopenharmony_ci geoLocationManager.getCurrentLocation(requestInfo) 63e41f4b71Sopenharmony_ci .then((location: geoLocationManager.Location) => { 64e41f4b71Sopenharmony_ci promptAction.showToast({ message: JSON.stringify(location) }); 65e41f4b71Sopenharmony_ci }) 66e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 67e41f4b71Sopenharmony_ci console.error(`Failed to get current location. Code is ${err.code}, message is ${err.message}`); 68e41f4b71Sopenharmony_ci }); 69e41f4b71Sopenharmony_ci } catch (err) { 70e41f4b71Sopenharmony_ci console.error(`Failed to get current location. Code is ${err.code}, message is ${err.message}`); 71e41f4b71Sopenharmony_ci } 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci @Entry 75e41f4b71Sopenharmony_ci @Component 76e41f4b71Sopenharmony_ci struct Index { 77e41f4b71Sopenharmony_ci build() { 78e41f4b71Sopenharmony_ci Row() { 79e41f4b71Sopenharmony_ci Column({ space: 10 }) { 80e41f4b71Sopenharmony_ci LocationButton({ 81e41f4b71Sopenharmony_ci icon: LocationIconStyle.LINES, 82e41f4b71Sopenharmony_ci text: LocationDescription.CURRENT_LOCATION, 83e41f4b71Sopenharmony_ci buttonType: ButtonType.Normal 84e41f4b71Sopenharmony_ci }) 85e41f4b71Sopenharmony_ci .padding({top: 12, bottom: 12, left: 24, right: 24}) 86e41f4b71Sopenharmony_ci .onClick((event: ClickEvent, result: LocationButtonOnClickResult) => { 87e41f4b71Sopenharmony_ci if (result === LocationButtonOnClickResult.SUCCESS) { 88e41f4b71Sopenharmony_ci // Obtain temporary authorization to get the location information without requesting the location permission. 89e41f4b71Sopenharmony_ci getCurrentLocationInfo(); 90e41f4b71Sopenharmony_ci } else { 91e41f4b71Sopenharmony_ci promptAction.showToast({message: 'Failed to obtain the location information.'}) 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci }) 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci .width('100%') 96e41f4b71Sopenharmony_ci } 97e41f4b71Sopenharmony_ci .height('100%') 98e41f4b71Sopenharmony_ci .backgroundColor(0xF1F3F5) 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci ``` 102