Tracking user movements

geo to appsync

In this section, you will use additional features of the Amazon Location Service to track and gather user movement around the circuit!

1. Create a Geofence collection

Head back to the console and click on Amazon Location Service. Expand the left side menu and click on Geofence collections, then Create geofence collection. Name the collection MonzaCircuit.(Select ‘yes’ for the simulated data question) Keep the default that will send your events to EventBridge with CloudWatch Logs as a target.

On the next page, for the MonzaCircuit geofence collection, click Add geofences. Download the monza.geo.json file below and upload it to the console via modal dialog.

2. Add permissions to your Amazon Cognito Identity Pool role

You will add some additional permissions to the same IAM role as before. From your cloud9 terminal:

amplify auth console

Select Identity Pool and follow the link to the AWS console. In the Cognito User Pool console, click on Edit identity pool.

cup ip

Take note of the name of the Authenticated role. This is the role that your authenticated users can leverage to access AWS resources from the app. Next, go to the AWS IAM console page and locate the role. Add a new inline policy called MapsEvaluate to the role:

{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Sid": "BatchEvaluate",
          "Effect": "Allow",
          "Action": [
              "geo:BatchEvaluateGeofences"
          ],
          "Resource": "arn:aws:geo:<region>:<account ID>:geofence-collection/MonzaCircuit"
      }
  ]
}

Replace <region> and <account ID> with your specific information.

3. Implement location update in your app.

In Cloud9, under the src folder, create a new folder called data. Upload the monza.geo.json file to this folder.

Install the AWS SDK Location client that your app will use to update your logged-in user geo-location.

yarn add @aws-sdk/client-location

Next, update the file src/pages/Map.tsx:

[ 🌟 ] src/pages/Map.tsx

You should now see a fully functional map centered on our main venue: the Autodromo Nazionale Monza racing circuit. Take a look around!

tracking

You can move the small user icon on the map to simulate a user movement on the map. Try moving the icon in and out of the overlay over the circuit!

Head back to the console and click on Amazon CloudWatch. Select Log groups in the left side menu and search for /aws/events/AmazonLocationMonitor-MonzaCircuit. Open the log group and look at some of the log streams. Any time a device or user enters/exits the geolocation, an event is added to cloudwatch logs.

event