# User's Current Location

A user's location is useful data to have on hand throughout a journey. In order to retrieve the users current location, this requires using the [Map control](../../web-controls/map-web-control) . The maps control provides the ability to get both the latitude and longitude of the user, as well as an address of the closest location that Google has to those coordinates. These outputs can then be stored as a variable and then sent to other systems or included as a part of your journey. For example, you can get the current location and use it to auto populate form fields or use the user's current location to get nearby POIs.

### Getting the users current location/address as a variable

This section will show how to get a current users location as a variable, both a user's geocoded location and a user's coordinates.&#x20;

To pass a users location as a variable, first create a 'Place Object' in AirData, and name it **Place.** This will create a Place type for the variable used to store the user's location.&#x20;

![appobjectPlace\_\_2\_.gif](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Fworking-with-the-users-current-location-v180-0?alt=media).gif)

Then add a variable of the type '**Place**' on the web page. Click on the '**+**' icon next to variables in the inspector, go to **objects**, and select **Place**. Name the variable **location**. Add another variable with the type '**Place**' and call it **geo\_location**.&#x20;

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-8786a5c47ac772ec498d91582d70105ddf2e0b43%2Fworking-with-the-users-current-location-v180-1.png?alt=media)

Next, add the maps control to the webpage. With the maps control selected, add the '**location**' variable to the center property and the **geo\_location** variable to the geocodedCenter property.  The maps control will automatically center at the user's current location which will then write those coordinates/addresses to those variables.

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-b82870dcf0bca9adb74897cfbe60bd55890fbec6%2Fworking-with-the-users-current-location-v180-2.png?alt=media)

Once the properties on the map control are configured, save the application and **Preview** the application. In the **App Preview**, the location will automatically default to 161 California Ave, but within a published application, the user will be prompted to allow access to their location for that application. This is required in order to get a user's location. The below are the outputs of both the **location** and **geo\_location** variables, that can then be used across the application or journey.&#x20;

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-ea644ff30a0455cd06472789740a64b0e9cee424%2Fworking-with-the-users-current-location-v180-3.png?alt=media)

**Note**: To split the **formattedAddress** into different address components (i.e. Address line, City, State, Zipcode), use the [SPLIT](../../airscript-expressions/text-functions/split) ) function to break the address apart.

### Display the current location of a user's device on a map

The maps control can be used to display the current location of a user on the map. This is can be used in applications where a user needs to see and pin their specific location or an application needs to get a user's location without any user input. By using the Maps control and the place object, Airkit will be able to pinpoint the user's device location. This section will walk through how to display the location of a user on the map.&#x20;

First add a Place object in AirData, and name it **Place**.&#x20;

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-9dc7f791742902b6e1179e9c02a5f62522544c3c%2Fworking-with-the-users-current-location-v180-4.gif?alt=media)

Then, add two variables with the type **Place** on the web page - **location** and **geo\_location**. This example will give the option to surface to location data objects.

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-22b4530bf373c9b9e646f11a387766e860179934%2Fworking-with-the-users-current-location-v180-5.png?alt=media)

The **location** variable will be used for the center property, which will return just the latitude and longitude. The **geo\_location** will be used for the geocodedCenter property, which will return the following object:

**Sample geo\_location**

```json
{   
  "latitude": 37.428706,   
  "longitude": -122.141921,   
  "formattedAddress": "161 California Ave, Palo Alto, CA 94306, USA",   
  "identifier": "<id>"   
}
```

The geocodedCenter will return the nearest address/place that Google has to the coordinates and provides a formatted address, latitude, longitude, and identifier.

Next, select the map control and configure the following properties:

* geocodedCenter: **geo\_location**
* center: **location**
* locationMarker: **location**

Once the properties are configured, save the application and go to the App Preview. The map should be displayed on the screen with a marker on the user's current location.&#x20;

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-e2c804026767a42b8cad102a3211973f034c8029%2Fworking-with-the-users-current-location-v180-6.png?alt=media)

The location can also be displayed as a location indicator. To configure a location indicator on a map, check the showIndicator property on the map control. The indicatorColor is also configurable as well.

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-971b20f4108c3d0116e6e30eb9a4ff5ef07a20e4%2Fworking-with-the-users-current-location-v180-7.png?alt=media)

This will add a location indicator that reflects the user's location.

![](https://2093598723-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC2DgLBPTrwoEhWaU24LL%2Fuploads%2Fgit-blob-bf1e149b556374d42ce22c9a36c1ed2aa0ef0416%2Fworking-with-the-users-current-location-v180-8.png?alt=media)

Note: When viewing the application in the App Preview, it will default to 161 California Ave, Palo Alto CA. In order to see the application work as intended, publish the application and view it through a mobile or desktop browser. The browser/user must allow the application to have access to the location in order to retrieve the user's location.
