Front-End Web & Mobile

Powering Travel through Geofences and Amazon Location Service

Amazon Location Service provides a cost efficient way to plug in high-quality maps and add location functionalities in applications. It empowers customers to visualize, geocode, provide navigation routing, and create geofences across locations of interest.

A geofence, sometimes known as an Area of Interest (AOI), is simply a polygon boundary representing an existing or custom-defined space, which allows users to perform analytics and push notifications based on the spatial intersections with other data points. Some examples include:

  • Delivery – A consumer at home receives near real-time notifications when a package is being delivered.  A warehouse operations manager receives a signal when any one of their trucks enters or exits a fulfillment center.
  • User engagement / geo-marketing – When walking or driving by your favorite store, you receive a coupon code valid for the next hour.
  • Safety / security – A manager of an indoor mall wants to understand consumer walking patterns and know when they enter prohibited areas.
  • Transit – A traveler wants to get near real-time updates when a train or bus enters or leaves a station.

In this post, we walk through a use case of how Amazon Location Service could be used to notify customers about the perks offered at a nearby gas station as they drive along a highway. We dive deep into how the geofences are created, visualized, and managed for this use case.

Use Case: AB Gas Stations

Let us assume a fictitious company, AB Gas Stations is a well-known fuel station chain built with convenience stores for the highways. They are spaced 200 miles apart along almost any prominent highway system in the US. Based on the new loyalty points strategy, the CEO of AB Gas Stations wants to provide the following benefits to their loyal customers who frequent the highways:

  • Delight AB’s customers with a mobile (loyalty) app that sends push notifications with the special menu or coffee of the day when they are five miles away from the gas station.
  • If there are no special items for the day at a gas station, the app would prompt them to take a break at the gas station and grab a free cookie.

AB Gas Stations has chosen Amazon Location Service to build this app for the following reasons:

  • The ease with which Amazon Location Service could be built and integrated into their existing mobile app.
  • The building costs for this solution are very competitive and attractive for the company long-term.

Solution

Figure 1 is the AWS Reference Architecture diagram used as the backdrop for this solution.

We follow the steps detailed in the Developer Guide to create geofences, link trackers, and send push notifications to AB’s loyalty customers. In the following steps, we show how to visualize and manage geofences.

Amazon Location Service Geofences and Location Tracking

Figure 1: Amazon Location Service Geofences and Location Tracking

1.      Map the locations of the Gas Stations through Geofences Collection

A GeoJSON is a file in JSON containing data that represents simple geographical features. These features can be simple points (e.g., latitude and longitude), line strings (e.g., streets), and polygons (collections of points that define a geography, such as a country border). In the case of a geofence, the GeoJSON file that we create contains polygons defining boundaries around our desired points. As shown in Figure 2, a pinpoint is dropped at the latitude and longitude coordinates of our gas station. The polygon drawn around it is the geofence.

In this walkthrough, we use https://geojson.io to drop gas station locations as pin points on the map, and draw areas to be fenced around them. We then save the GeoJSON file(s) to be created. Note that geofences in Amazon Location Service can only be polygons, not points (such as latitude/longitudes), so any pin points dropped on the maps will need to be removed from the JSON before import into Amazon Location Service.

Create and Visualize Geofences - GeoJSON file

Figure 2: Create and Visualize Geofences – GeoJSON file

For our use case, we must create a collection of geofences with Amazon Location Service to represent all of the gas station areas of interest along the highways. We follow the steps detailed in the Developer Guide to create a geofence collection. Then, we import one or more GeoJSON files that we created earlier into this Geofence Collection.  Using the Developer Guide, we see that we can create a geofence collection using the AWS Console, Amazon Location APIs, or AWS CLI.  The following example CLI commands will create a collection and then import a geofence into the collection.  Note, we only need the “coordinates” key from the GeoJSON for our data input.

$ aws location create-geofence-collection --collection-name MyGeofenceCollection 
$ aws location put-geofence --collection-name MyGeofenceCollection \
    --geofence-id geofence-01 \
    --geometry 'Polygon=[[[-5.716667, -15.933333],[-14.416667, -7.933333],[-12.316667, -37.066667],[-5.716667, -15.933333]]]'

2.      Link the Tracker to the Geofence Collection

A tracker receives location updates from devices capable of transmitting a location update, such as a mobile phone or GPS receivers.  To create a tracker and associate it with our geofence collection using the AWS CLI, you can start with the following commands.

$ aws location create-tracker --tracker-name MyTracker
$ aws location associate-tracker-consumer --tracker-name MyTracker \
    --consumer-arn ‘arn:aws:geo:us-west-2:123456789012:geofence-collection/MyGeofenceCollection’

The AB loyalty app is installed on the customer’s handheld devices, such as a mobile phone. These devices send their location updates to Amazon Location Service through the Amazon SDK available on the app. The device’s location updates are then evaluated against the geofences to determine the customer’s location with respect to the geofences. In our case, when customers are five miles away from an AB gas station and cross into geofenced areas, “ENTER” and “EXIT” events are generated. We use Amazon EventBridge, a scalable serverless event bus, to react to these events and send push notifications to the mobile devices through Amazon Simple Notification Service (SNS). For more information on how Amazon EventBridge works with Amazon Location Service, refer to the Developer Guide.

3.      Management and Visualization of Geofences

Note that the Amazon Location Service Console provides a built-in visual and interactive tool (Figure 3) for you to preview the default maps, search for points of interest, calculate routes, simulate tracking devices, and draw geofences. This tool is handy to try out the geofencing capabilities in Amazon Location Service and get started through the AWS Management Console.

The Interactive Tool in Amazon Location Service Console

Figure 3: The Interactive Tool in Amazon Location Service Console

In our use case, AB Gas stations may open many gas stations over the years. This calls for managing the geofences effectively. Since GeoJSON files have many thousands of polygons defined and can get very large, the following suggestions can help manage them at scale:

  • Break the GeoJSON files into smaller chunks that clarify the application scope, such as city-based, county-based, etc. Name the files with an intuitive naming scheme. This will help make the geofences’ visualization more manageable.
  • Manage the versioning on the GeoJSON files. This can be done by using the Amazon Simple Storage Service (S3) object store or AWS CodeCommit. AWS CodeCommit supports the standard functionality of Git, thus it works seamlessly with your existing Git-based tools.
  • For visualization and editing, bring the GeoJSON files back into a tool such as https://geojson.io or https://geojson.tools/.
  • If you need more extensive Geographic Information System (GIS) platforms, explore data providers and platforms like Esri and HERE.

Conclusion

This post showed how just a few clicks lets us set up, visualize, and manage geofences for a sample use case. We highlighted the steps required for this specific solution, and demonstrated the ease with which push notifications could be setup and sent to the customers’ devices.

If you are interested in a similar solution for your organization, don’t hesitate to reach out to us.

About the authors

Joy Fasnacht

Joy Fasnacht

Joy Fasnacht is a Solutions Architect Manager at AWS. She came to AWS as a Senior Solutions Architect in early 2018 to help Enterprise customers across the Midwest US in their cloud transformation journeys. She has held many software engineering and tech leadership roles over the years, and explores maps and mapping technologies whenever she can.

Shailaja Suresh

Shailaja Suresh

Shailaja Suresh is a Senior Solutions Architect at AWS, with professional experience in software product architecture, strategy, and delivery. She offers prescriptive technical guidance to customers on their AWS Cloud journey. With a wide range of engineering skills, Shailaja has held roles as a software engineer, architect, lead, and project manager. Shailaja strongly believes in empowering teams through mentorship and coaching.

Dave Bianco

Dave Bianco is a Sr Geospatial Architect with AWS Professional Services. With over 20 years of experience in the field, he supports customers of all sizes as they transition to cloud-native geospatial technologies. His focus is on data management, data pipelines, and backend engineering.