

# Sample Geofencing and Tracking mobile application
<a name="geofence-tracking-tutorials"></a>

This topic covers tutorials designed to demonstrate the key features of using the Amazon Location geofences and trackers in a mobile application. The applications demonstrate how a tracker and geofence interact using a combination of Lambda, AWS IoT and Amazon Location features. There are two tutorials available.
+ [Sample tracking and geofencing application for Android](https://docs.aws.amazon.com//location/previous/developerguide/qs-android-tracking.html), and you can clone the project files from GitHub: [https://github.com/aws-geospatial/amazon-location-samples-android/tree/main/tracking-with-geofence-notifications](https://github.com/aws-geospatial/amazon-location-samples-android/tree/main/tracking-with-geofence-notifications).
+ [Sample tracking and geofencing application for iOS](https://docs.aws.amazon.com//location/previous/developerguide/qs-ios-tracking.html), and you can clone the project files from GitHub: [https://github.com/aws-geospatial/amazon-location-samples-ios/tree/main/tracking-with-geofence-notifications](https://github.com/aws-geospatial/amazon-location-samples-ios/tree/main/tracking-with-geofence-notifications).

# Sample tracking and geofence application for Android
<a name="qs-android-tracking"></a>

This topic covers the Android tutorial designed to demonstrate the key features of using the Amazon Location geofences and trackers in a mobile application. The applications demonstrate how a tracker and geonfence interact using a combination of Lambda, AWS IoT and Amazon Location features.

**Topics**
+ [Tutorial: Create Amazon Location resources for your app](qs-android-tracking-resources.md)
+ [Tutorial: Create a Geofence Collection](qs-android-tracking-geofence.md)
+ [Tutorial: Link a tracker to a geofence collection](qs-android-tracking-link-geofence.md)
+ [Tutorial: Use AWS Lambda with MQTT](qs-android-tracking-lambda.md)
+ [Tutorial: Set up the sample app code](qs-android-tracking-sample-app-code.md)
+ [Tutorial: Use the sample app](qs-android-tracking-use.md)

# Tutorial: Create Amazon Location resources for your app
<a name="qs-android-tracking-resources"></a>

To begin you will need to create the required Amazon Location resources. These resources will be essential for the functionality of the application and executing the provided code snippets.

**Note**  
If you haven't created an AWS account, follow the instructions in the [AWS account managment](https://docs.aws.amazon.com/accounts/latest/reference/welcome-first-time-user.html) user guide.

To begin you will need to create a Amazon Cognito Identity Pool Id, use the following procedure:

1. Open the [Amazon Cognito console](https://console.aws.amazon.com/cognito/v2/) and select **Identity pools** from the left side menu, then select **Create Identity pool**. 

1. Make sure **Guest Access** is checked, and press **Next** to contiue.

1. Next create a new IAM role or Use an existing IAM role.

1. Enter an Identity pool name, and make sure Identity Pool has access to Amazon Location `(geo)`resources for the map and tracker you will be creating nin the next procedure.

Next you need to create and style a map in the AWS Amazon Location console, use the following procedure:

1. Navigate to the [Maps section](https://console.aws.amazon.com/location/maps/home) of the Amazon Location console and select **Create Map**.

1. Give the new map a **Name** and **Description**. Record the name you assign, as it is used later in the tutorial.

1. When choosing a map style, consider the map data provider. Refer to section 82 of the [AWS service terms](http://aws.amazon.com/service-terms) for more details.

1. Accept the [Amazon Location Terms and Conditions](https://aws.amazon.com/service-terms/#:~:text=82.%20Amazon%20Location%20Service), then select **Create Map**, to finish the map creation process.

Next you need to create a tracker in the Amazon Location console, use the following procedure:

1.  Open the [Maps section](https://console.aws.amazon.com/location/trackers) in the Amazon Location console.

1. Choose **Create tracker**.

1. Fill in the required fields. Make note of the tracker's **Name** as it will be refrenced throughout this tutoiral.

1. Under the **Position filtering** field, choose the option that best fits how you intend to use your tracker resource. If you do not set Position filtering, the default setting is `TimeBased`. For more information, see [Trackers](), and [PositionFiltering]() in the Amazon Location API Reference.

1.  Choose **Create tracker** to finish creating the tracker.

# Tutorial: Create a Geofence Collection
<a name="qs-android-tracking-geofence"></a>

Now will you create a geofence collection. You can use either the console, API or CLI. The following procedures walk you through each option.
+ Create a geofence collection using the Amazon Location console:

  1.  Open the [Geofence Collections](https://console.aws.amazon.com/location/geofencing/home#/) section of the Amazon Location console.

  1. Choose **Create geofence collection**.

  1. Provide a name and description for the collection.

  1. Under the EventBridge rule with Amazon CloudWatch as a target, you can create an optional EventBridge rule to get started reacting to geofence events. This enables Amazon Location to publish events to Amazon CloudWatch Logs.

  1. Press the **Create geofence collection** to finish creating the collection.
+ Create a geofence collection using the Amazon Location API:

  Use the [CreateGeofenceCollection](https://docs.aws.amazon.com/location/previous/APIReference/API_CreateGeofenceCollection.html) operation from the Amazon Location Geofences APIs. The following example uses an API request to create a geofence collection called `GEOCOLLECTION_NAME`.

  ```
  POST /geofencing/v0/collections
  Content-type: application/json
  
  {
     "CollectionName": "GEOCOLLECTION_NAME",
     "Description": "Geofence collection 1 for shopping center",
     "Tags": { 
        "Tag1" : "Value1"
     }
  }
  ```
+ Create a geofence collection using AWS CLI commands:

  Use the `create-geofence-collection` command. The following example uses an AWS CLI to create a geofence collection called `GEOCOLLECTION_NAME`. For more information on using the AWS CLI, see the [AWS Command Line Interface Documentation](https://docs.aws.amazon.com/cli/).

  ```
  aws location \
    create-geofence-collection \
    --collection-name "ExampleGeofenceCollection" \
    --description "Shopping center geofence collection" \
    --tags Tag1=Value1
  ```

# Tutorial: Link a tracker to a geofence collection
<a name="qs-android-tracking-link-geofence"></a>

To link a tracker to a geofence collection you can use either the console, API, or CLI. The following procedures walk you through each option.

Link a tracker resource to a geofence collection using the Amazon Location Service console:

1.  Open the Amazon Location console.

1.  In the left navigation pane, choose **Trackers**.

1.  Under **Device Trackers**, select the name link of the target tracker.

1.  Under **Linked Geofence Collections**, choose **Link Geofence Collection**.

1.  In the **Linked Geofence Collection window**, select a geofence collection from the dropdown menu.

1.  Choose **Link**.

1.  After you link the tracker resource, it will be assigned an Active status.

Link a tracker resource to a geofence collection using the Amazon Location APIs:

Use the `AsssociateTrackerConsumer` operation from the Amazon Location Trackers APIs. The following example uses an API request that associates an `ExampleTracker` with a geofence collection using its Amazon Resource Name (ARN).

```
        POST /tracking/v0/trackers/ExampleTracker/consumers
        Content-type: application/json
        {
           "ConsumerArn": "arn:aws:geo:us-west-2:123456789012:geofence-collection/GOECOLLECTION_NAME"
            }
```

Link a tracker resource to a geofence collection using AWS CLI commands:

Use the `associate-tracker-consumer ` command. The following example uses an AWS CLI to create a geofence collection called `GOECOLLECTION_NAME`.

```
aws location \
associate-tracker-consumer \
    --consumer-arn "arn:aws:geo:us-west-2:123456789012:geofence-collection/GOECOLLECTION_NAME" \
    --tracker-name "ExampleTracker"
```

# Tutorial: Use AWS Lambda with MQTT
<a name="qs-android-tracking-lambda"></a>

In order to create a connection between AWS IoT and Amazon Location, you need a Lambda function to process messages forwarded by EventBridge CloudWatch events. This function will extract any positional data, format it for Amazon Location, and submit it through the Amazon Location Tracker API.

The following procedure shows you how to create this function through the Lambda console:

1. Open the [console](https://console.aws.amazon.com/lambda/).

1. From the left navigation, choose **Functions**.

1. Then choose **Create Function**, and make sure that the **Author from scratch** option is selected.

1.  provide a **Function name**, and for the **Runtime** option, choose Node.js 16.x.

1. Choose **Create function**.

1. Open the **Code tab** to access the editor.

1. Overwrite the placeholder code in the `index.js` file with the following:

   ```
               const AWS = require('aws-sdk')
               const iot = new AWS.Iot();
               exports.handler =  function(event) {
                 console.log("event===>>>", JSON.stringify(event));
                 var param = {
                   endpointType: "iot:Data-ATS"
                 };
                 iot.describeEndpoint(param, function(err, data) {
                   if (err) {
                     console.log("error===>>>", err, err.stack); // an error occurred
                   } else {
                     var endp = data['endpointAddress'];
                     const iotdata = new AWS.IotData({endpoint: endp});    
                     const trackerEvent = event["detail"]["EventType"];
                     const src = event["source"];
                     const time = event["time"];
                     const gfId = event["detail"]["GeofenceId"];
                     const resources = event["resources"][0];  
                     const splitResources = resources.split(".");  
                     const geofenceCollection = splitResources[splitResources.length - 1];
                     const coordinates = event["detail"]["Position"];                               
                     const deviceId = event["detail"]["DeviceId"]; 
                     console.log("deviceId===>>>", deviceId);
                     const msg =  {
                         "trackerEventType" : trackerEvent,
                         "source" : src,
                         "eventTime" : time,
                         "geofenceId" : gfId,
                         "coordinates": coordinates, 
                         "geofenceCollection": geofenceCollection
                       };
                     const params = {
                       topic: `${deviceId}/tracker`,
                       payload: JSON.stringify(msg),
                       qos: 0
                     };
                     iotdata.publish(params, function(err, data) {
                         if (err) {
                           console.log("error===>>>", err, err.stack); // an error occurred
                         } else {
                           console.log("Ladmbda triggered===>>>", trackerEvent);  // successful response 
                         }
                     }); 
                   }
                 });
               }
   ```

1. Press the **Deploy** to save the updated function.

1. Next open the **Configuration** tab.

1. In the **Triggers** section, press the **Add Trigger** button.

1. Select **EventBridge (CloudWatch Events)** in Source field.

1. Select the **Existing Rules** option.

1. Enter the rule name, for example `AmazonLocationMonitor-GEOFENCECOLLECTION_NAME`.

1. Press the **Add** button.

1.  This will also attach **Resource-based policy statements** in the permissions tab

Now you will set up the MQTT Test Client using AWS IoT, use the following procedure:

1. Open the [https://console.aws.amazon.com/iot/](https://console.aws.amazon.com/iot/).

1.  In the left navigation pane, select the **MQTT test client**.

1.  You'll see a section titled **MQTT test client** where you can configure your MQTT connection.

1.  After configuring the necessary settings, click on the **Connect** button to establish a connection to the MQTT broker using the provided parameters.

1.  Record endpoint, as it is used later in the tutoiral.

   Once connected to the test client, you can subscribe to MQTT topics or publish messages to topics using the respective input fields provided in the MQTT test client interface. Next you will create an AWS IoT policy.

1.  On the left side menu, under **Manage** expand **Security** option and click on **Policies**.

1.  Click on **Create Policy** button.

1.  Enter a policy name.

1.  On **Policy Document** select **JSON** tab.

1. Copy paste the policy shown below, but make sure to update all elements with your `REGION` and `ACCOUNT_ID`:

   ```
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Action": [
           "iot:Connect",
           "iot:Publish",
           "iot:Subscribe",
           "iot:Receive"
         ],
         "Resource": [
           "arn:aws:iot:REGION:ACCOUNT_ID:client/${cognito-identity.amazonaws.com:sub}",
           "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}",
           "arn:aws:iot:REGION:ACCOUNT_ID:topicfilter/${cognito-identity.amazonaws.com:sub}/*",
           "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}/tracker"
         ],
         "Effect": "Allow"
       }
     ]
   }
   ```

1. Select the **Create** button to finish.

After completing the previous procedure, you will now update the permissions for the guest role as follows:

1. Navigate to Amazon Cognito and open your identity pool. Then, proceed to user access and select the guest role.

1. Click on permission policies to enable editing.

   ```
   {
       'Version': '2012-10-17		 	 	 ',
       'Statement': [
           {
               'Action': [
                   'geo:GetMap*',
                   'geo:BatchUpdateDevicePosition',
                   'geo:BatchEvaluateGeofences',
                   'iot:Subscribe',
                   'iot:Publish',
                   'iot:Connect',
                   'iot:Receive',
                   'iot:AttachPrincipalPolicy',
                   'iot:AttachPolicy',
                   'iot:DetachPrincipalPolicy',
                   'iot:DetachPolicy'
               ],
               'Resource': [
                   'arn:aws:geo:us-east-1:{USER_ID}:map/{MAP_NAME}',
                   'arn:aws:geo:us-east-1:{USER_ID}:tracker/{TRACKER_NAME}',
                   'arn:aws:geo:us-east-1:{USER_ID}:geofence-collection/{GEOFENCE_COLLECTION_NAME}',
                   'arn:aws:iot:us-east-1:{USER_ID}:client/${cognito-identity.amazonaws.com:sub}',
                   'arn:aws:iot:us-east-1:{USER_ID}:topic/${cognito-identity.amazonaws.com:sub}',
                   'arn:aws:iot:us-east-1:{USER_ID}:topicfilter/${cognito-identity.amazonaws.com:sub}/*',
                   'arn:aws:iot:us-east-1:{USER_ID}:topic/${cognito-identity.amazonaws.com:sub}/tracker'
               ],
               'Effect': 'Allow'
           },
           {
               'Condition': {
                   'StringEquals': {
                       'cognito-identity.amazonaws.com:sub': '${cognito-identity.amazonaws.com:sub}'
                   }
               },
               'Action': [
                   'iot:AttachPolicy',
                   'iot:DetachPolicy',
                   'iot:AttachPrincipalPolicy',
                   'iot:DetachPrincipalPolicy'
               ],
               'Resource': [
                   '*'
               ],
               'Effect': 'Allow'
           }
       ]
   }
   ```

1. With the above policy changes, all necessary AWS resources are now configured appropriately for the application.

# Tutorial: Set up the sample app code
<a name="qs-android-tracking-sample-app-code"></a>

This page provides a sample Android application code that demonstrates how to integrate the tracking capabilities of Amazon Location Service into your mobile applications. 

1. Clone this repository: [https://github.com/aws-geospatial/amazon-location-samples-android/tree/main/tracking-with-geofence-notifications](https://github.com/aws-geospatial/amazon-location-samples-android/tree/main/tracking-with-geofence-notifications) to your local machine.

1. Open the `AmazonSampleSDKApp` project in Android Studio.

1. Build and run the app on your Android device or emulator.

# Tutorial: Use the sample app
<a name="qs-android-tracking-use"></a>

This guide walks you through the process of setting up and using the sample Android tracking application. By following the step-by-step instructions outlined in this section, you'll learn how to configure the necessary AWS resources, integrate the tracking functionality into the sample app, and deploy it to your Android device. 

To use the sample follow these procedures:
+ **Create a `custom.properties`**:

  To configure your `custom.properties` file, follow these steps:

  1. Open your preferred text editor or IDE.

  1. Create a new file.

  1. Save the file with the name `custom.properties`.

  1. Update the `custom.properties` with the following code sample, and replace the `MQTT_END_POINT`, `POLICY_NAME`, `GEOFENCE_COLLECTION_NAME`, and `TOPIC_TRACKER` with your resoucre names:

     ```
     MQTT_END_POINT=YOUR_END_POINT.us-east-1.amazonaws.com
     POLICY_NAME=YOUR_POLICY
     GEOFENCE_COLLECTION_NAME=YOUR_GEOFENCE
     TOPIC_TRACKER=YOUR_TRACKER
     ```

  1. Clean and Rebuild the project. After this, you can run the project.
+ **Sign In**:

  To sign in to the application, follow the below steps:

  1. Press the **Sign In** button.

  1. Provide an **Identity Pool Id**, **Tracker name**, and a **Map name**.

  1. Press **Sign In** again to finish.
+ **Manage Filters**:

  Open the configuration screen, and perform the following:

  1. Toggle filters on or off using the switch UI.

  1. Update Time and Distance filters when needed.
+ **Tracking Operations:**

  Open the tracking screen and perform the following:
  + You can start and stop tracking in foreground, background, or in battery-saver mode by pressing the respective buttons.

# Sample tracking and geofencing application for iOS
<a name="qs-ios-tracking"></a>

This topic covers the iOS tutorial designed to demonstrate the key features of using the Amazon Location geofences and trackers in a mobile application. The applications demonstrate how a tracker and geonfence interact using a combination of Lambda, AWS IoT and Amazon Location features.

**Topics**
+ [Tutorial: Create Amazon Location resources for your app](qs-ios-tracking-resources.md)
+ [Tutorial: Create a Geofence Collection](qs-ios-tracking-geofence.md)
+ [Tutorial: Link a tracker to a geofence collection](qs-ios-tracking-link-geofence.md)
+ [Tutorial: Use AWS Lambda with MQTT](qs-ios-tracking-lambda.md)
+ [Tutorial: Set up sample app code](qs-ios-tracking-setup-sample.md)
+ [Tutorial: Use the sample app](qs-ios-tracking-usage.md)

# Tutorial: Create Amazon Location resources for your app
<a name="qs-ios-tracking-resources"></a>

To begin you will need to create the required Amazon Location resources. These resources will be essential for the functionality of the application and executing the provided code snippets.

**Note**  
If you haven't created an AWS account, follow the instructions in the [AWS account managment](https://docs.aws.amazon.com/accounts/latest/reference/welcome-first-time-user.html) user guide.

To begin you will need to create a Amazon Cognito Identity Pool Id, use the following procedure:

1. Open the [Amazon Cognito console](https://console.aws.amazon.com/cognito/v2/) and select **Identity pools** from the left side menu, then select **Create Identity pool**. 

1. Make sure **Guest Access** is checked, and press **Next** to contiue.

1. Next create a new IAM role or Use an existing IAM role.

1. Enter an Identity pool name, and make sure Identity Pool has access to Amazon Location `(geo)`resources for the map and tracker you will be creating nin the next procedure.

Next you need to create and style a map in the AWS Amazon Location console, use the following procedure:

1. Navigate to the [Maps section](https://console.aws.amazon.com/location/maps/home) of the Amazon Location console and select **Create Map**.

1. Give the new map a **Name** and **Description**. Record the name you assign, as it is used later in the tutorial.

1. When choosing a map style, consider the map data provider. Refer to section 82 of the [AWS service terms](http://aws.amazon.com/service-terms) for more details.

1. Accept the [Amazon Location Terms and Conditions](https://aws.amazon.com/service-terms/#:~:text=82.%20Amazon%20Location%20Service), then select **Create Map**, to finish the map creation process.

Next you need to create a tracker in the Amazon Location console, use the following procedure:

1.  Open the [Maps section](https://console.aws.amazon.com/location/trackers) in the Amazon Location console.

1. Choose **Create tracker**.

1. Fill in the required fields. Make note of the tracker's **Name** as it will be refrenced throughout this tutoiral.

1. Under the **Position filtering** field, choose the option that best fits how you intend to use your tracker resource. If you do not set Position filtering, the default setting is `TimeBased`. For more information, see [Start tracking](location/previous/developerguide/start-tracking.html), and start-tracking.html [PositionFiltering](https://docs.aws.amazon.com/location/previous/APIReference/API_BatchUpdateDevicePosition.html) in the Amazon Location API Reference.

1.  Choose **Create tracker** to finish creating the tracker.

# Tutorial: Create a Geofence Collection
<a name="qs-ios-tracking-geofence"></a>

Now will you create a geofence collection. You can use either the console, API or CLI. The following procedures walk you through each option.
+ Create a geofence collection using the Amazon Location console:

  1.  Open the [Geofence Collections](https://console.aws.amazon.com/location/geofencing/home#/) section of the Amazon Location console.

  1. Choose **Create geofence collection**.

  1. Provide a name and description for the collection.

  1. Under the EventBridge rule with Amazon CloudWatch as a target, you can create an optional EventBridge rule to get started reacting to geofence events. This enables Amazon Location to publish events to Amazon CloudWatch Logs.

  1. Press the **Create geofence collection** to finish creating the collection.
+ Create a geofence collection using the Amazon Location API:

  Use the [CreateGeofenceCollection](https://docs.aws.amazon.com/location/previous/APIReference/API_CreateGeofenceCollection.html) operation from the Amazon Location Geofences APIs. The following example uses an API request to create a geofence collection called `GEOCOLLECTION_NAME`.

  ```
  POST /geofencing/v0/collections
  Content-type: application/json
  
  {
     "CollectionName": "GEOCOLLECTION_NAME",
     "Description": "Geofence collection 1 for shopping center",
     "Tags": { 
        "Tag1" : "Value1"
     }
  }
  ```
+ Create a geofence collection using AWS CLI commands:

  Use the `create-geofence-collection` command. The following example uses an AWS CLI to create a geofence collection called `GEOCOLLECTION_NAME`. For more information on using the AWS CLI, see the [AWS Command Line Interface Documentation](https://docs.aws.amazon.com/cli/).

  ```
  aws location \
    create-geofence-collection \
    --collection-name "ExampleGeofenceCollection" \
    --description "Shopping center geofence collection" \
    --tags Tag1=Value1
  ```

# Tutorial: Link a tracker to a geofence collection
<a name="qs-ios-tracking-link-geofence"></a>

To link a tracker to a geofence collection you can use either the console, API, or CLI. The following procedures walk you through each option.

Link a tracker resource to a geofence collection using the Amazon Location Service console:

1.  Open the Amazon Location console.

1.  In the left navigation pane, choose **Trackers**.

1.  Under **Device Trackers**, select the name link of the target tracker.

1.  Under **Linked Geofence Collections**, choose **Link Geofence Collection**.

1.  In the **Linked Geofence Collection window**, select a geofence collection from the dropdown menu.

1.  Choose **Link**.

1.  After you link the tracker resource, it will be assigned an Active status.

Link a tracker resource to a geofence collection using the Amazon Location APIs:

Use the `AsssociateTrackerConsumer` operation from the Amazon Location Trackers APIs. The following example uses an API request that associates ExampleTracker with a geofence collection using its Amazon Resource Name (ARN).

```
POST /tracking/v0/trackers/ExampleTracker/consumers
Content-type: application/json
    {
       "ConsumerArn": "arn:aws:geo:us-west-2:123456789012:geofence-collection/GEOCOLLECTION_NAME"
    }
```

Llink a tracker resource to a geofence collection using AWS CLI commands:

Use the `associate-tracker-consumer ` command. The following example uses an AWS CLI to create a geofence collection called `GEOCOLLECTION_NAME`.

```
aws location \
    associate-tracker-consumer \
        --consumer-arn "arn:aws:geo:us-west-2:123456789012:geofence-collection/GEOCOLLECTION_NAME" \
        --tracker-name "ExampleTracker"
```

# Tutorial: Use AWS Lambda with MQTT
<a name="qs-ios-tracking-lambda"></a>

In order to create a connection between AWS IoT and Amazon Location, you need a Lambda function to process messages forwarded by EventBridge CloudWatch events. This function will extract any positional data, format it for Amazon Location, and submit it through the Amazon Location Tracker API.

The following procedure shows you how to create this function through the Lambda console:

1. Open the [console](https://console.aws.amazon.com/lambda/).

1. From the left navigation, choose **Functions**.

1. Then choose **Create Function**, and make sure that the **Author from scratch** option is selected.

1.  provide a **Function name**, and for the **Runtime** option, choose Node.js 16.x.

1. Choose **Create function**.

1. Open the **Code tab** to access the editor.

1. Overwrite the placeholder code in the `index.js` file with the following:

   ```
               const AWS = require('aws-sdk')
               const iot = new AWS.Iot();
               exports.handler =  function(event) {
                 console.log("event===>>>", JSON.stringify(event));
                 var param = {
                   endpointType: "iot:Data-ATS"
                 };
                 iot.describeEndpoint(param, function(err, data) {
                   if (err) {
                     console.log("error===>>>", err, err.stack); // an error occurred
                   } else {
                     var endp = data['endpointAddress'];
                     const iotdata = new AWS.IotData({endpoint: endp});    
                     const trackerEvent = event["detail"]["EventType"];
                     const src = event["source"];
                     const time = event["time"];
                     const gfId = event["detail"]["GeofenceId"];
                     const resources = event["resources"][0];  
                     const splitResources = resources.split(".");  
                     const geofenceCollection = splitResources[splitResources.length - 1];
                     const coordinates = event["detail"]["Position"];                               
                     const deviceId = event["detail"]["DeviceId"]; 
                     console.log("deviceId===>>>", deviceId);
                     const msg =  {
                         "trackerEventType" : trackerEvent,
                         "source" : src,
                         "eventTime" : time,
                         "geofenceId" : gfId,
                         "coordinates": coordinates, 
                         "geofenceCollection": geofenceCollection
                       };
                     const params = {
                       topic: `${deviceId}/tracker`,
                       payload: JSON.stringify(msg),
                       qos: 0
                     };
                     iotdata.publish(params, function(err, data) {
                         if (err) {
                           console.log("error===>>>", err, err.stack); // an error occurred
                         } else {
                           console.log("Ladmbda triggered===>>>", trackerEvent);  // successful response 
                         }
                     }); 
                   }
                 });
               }
   ```

1. Press the **Deploy** to save the updated function.

1. Next open the **Configuration** tab.

1. In the **Triggers** section, press the **Add Trigger** button.

1. Select **EventBridge (CloudWatch Events)** in Source field.

1. Select the **Existing Rules** option.

1. Enter the rule name, for example `AmazonLocationMonitor-GEOFENCECOLLECTION_NAME`.

1. Press the **Add** button.

1.  This will also attach **Resource-based policy statements** in the permissions tab

Now you will set up the AWS IoT MQTT Test Client, use the following procedure:

1. Open the [https://console.aws.amazon.com/iot/](https://console.aws.amazon.com/iot/).

1.  In the left navigation pane, select the **MQTT test client**.

1.  You'll see a section titled **MQTT test client** where you can configure your MQTT connection.

1.  After configuring the necessary settings, click on the **Connect** button to establish a connection to the MQTT broker using the provided parameters.

1.  Record endpoint, as it is used later in the tutoiral.

   Once connected to the test client, you can subscribe to MQTT topics or publish messages to topics using the respective input fields provided in the MQTT test client interface. Next you will create an AWS IoT policy.

1.  On the left side menu, under **Manage** expand **Security** option and click on **Policies**.

1.  Click on **Create Policy** button.

1.  Enter a policy name.

1.  On **Policy Document** select **JSON** tab.

1. Copy paste the policy shown below, but make sure to update all elements with your `REGION` and `ACCOUNT_ID`:

   ```
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Action": [
           "iot:Connect",
           "iot:Publish",
           "iot:Subscribe",
           "iot:Receive"
         ],
         "Resource": [
           "arn:aws:iot:REGION:ACCOUNT_ID:client/${cognito-identity.amazonaws.com:sub}",
           "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}",
           "arn:aws:iot:REGION:ACCOUNT_ID:topicfilter/${cognito-identity.amazonaws.com:sub}/*",
           "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}/tracker"
         ],
         "Effect": "Allow"
       }
     ]
   }
   ```

1. Select the **Create** button to finish.

# Tutorial: Set up sample app code
<a name="qs-ios-tracking-setup-sample"></a>

In order to setup the sample code you must have the following tools installed:
+ Git
+ XCode 15.3 or Later
+ iOS Simulator 16 or later

Use this procedure to set up the sample app code:

1. Clone the git repository from this URL: [https://github.com/aws-geospatial/amazon-location-samples-ios/tree/main/tracking-with-geofence-notifications](https://github.com/aws-geospatial/amazon-location-samples-ios/tree/main/tracking-with-geofence-notifications).

1.  Open the `AWSLocationSampleApp.xcodeproj` project file.

1.  Wait for the package resolution process to finish.

1. On the project navigation menu rename `ConfigTemplate.xcconfig` to `Config.xcconfig` and fill in the following values:

   ```
   IDENTITY_POOL_ID = `YOUR_IDENTITY_POOL_ID`
   MAP_NAME = `YOUR_MAP_NAME`
   TRACKER_NAME = `YOUR_TRACKER_NAME`
   WEBSOCKET_URL = `YOUR_MQTT_TEST_CLIENT_ENDPOINT`
   GEOFENCE_ARN = `YOUR_GEOFENCE_COLLECTION_NAME`
   ```

# Tutorial: Use the sample app
<a name="qs-ios-tracking-usage"></a>

After setting up the sample code you can now run the app on an iOS simulator or a physical device.

1. Build and run the app.

1. The app will ask you for location and notification permissions. You need to allow them.

1. Press the **Cognito Configuration** button.

1. Save the configuration.

1. You can now see the Filter options for time, distance and accuracy. Use them as per your need.

1. Go to **Tracking** tab in the app and you will see the map and **Start Tracking** button.

1. If you have installed the app on a simulator you may want to simulate location changes. This can be done in **Features** under the **Location** menu option. For example select **Features**, then **Location**, then **Freeway Drive**.

1. Press the **Start Tracking** button. You should see the tracking points on the map.

1. The app is also tracking the locations in the background. So, when you move the app in the background it will ask for your permission to continue tracking in background mode.

1. You can stop the tracking by tapping on **Stop Tracking** button.