A geofence is a virtual perimeter, or a fence, around a geographic, real-world area of interest. In e-commerce, these areas can be used for different purposes, from personalization to delivery cost or time calculation.
One real example from my previous e-commerce projects was related to warehouse selection strategy. It used customer address data to determine the most cost-efficient way to source a product and move it to the point of sale for local pickup or deliver it to the customer address.

When the product is available at warehouse A, there is no problem: that warehouse is closest to the customer. However, if the product is only available at another warehouse, for example, warehouse B, the cost of moving the product to the customer location or a local store for pickup may be too high for the seller. In that project, the delivery price was always fixed for all products, all customers, and all regions served.
The simplest way to deal with customer and warehouse locations is to use postal codes.

However, this approach is not good for the following reasons:
- Using tables makes the system too complex in terms of management. Generally, postal codes cover relatively small areas. To define warehouses and delivery areas using postal codes, you need to deal with a very large amount of data. Using primary and secondary warehouses doubles that.
- Using business rules does not help here because the postal code system is generally too simple. You cannot group postal codes together by delivery cost just because all these areas are close to each other. Some postal codes should not be grouped because there is a river between them and no bridges nearby.
Geofencing helps with this task. Using a simple tool, you can mark areas on the map and process these areas differently. All customer locations inside the marked areas will be treated similarly. If you need to add an exception, you can create a new polygon and put it on top of the existing one. For any single address, you will have a list of areas. In the example above, there are two areas: red and black. All warehouses linked to the red area are primary for all customers inside this area. All warehouses linked to the black area are secondary for red area customers and primary for black area customers.
Solution
There are three components in the system:
- Storefront geofencing module
- Geofence editor
- Geofencing API

Storefront module
The storefront geofencing module is integrated into the customer address form. When the customer creates or updates the address, the following operations are performed:
- Geocoding. The latitude and longitude are calculated for the address. The Google Maps Geocoding API is used to get a latitude/longitude point to determine, at the next phase, whether it is within one of your fences. Take into account that there are usage limits in the free version of the Google Geocoding API, namely 2,500 free requests per day and 50 requests per second.
- List all fences that contain the point. The Geofencing API is used to retrieve the fences. The Geofencing API is a service component of the solution.
The list can be used in various components, such as:
- Delivery cost calculation
- Product availability
- Product price
- Delivery price
- Taxes, etc.
Geofence editor

The geofence editor is a Google Maps-based web application that uses the Geofencing API for storing and retrieving data. Using the geofence editor, you are able to:
- Create a polygon to define the area
- Name the polygon and save it
The editor is front-end-only; it uses the Geofencing API for uploading fences to the server and reading them from there.
Geofencing API
The architecture involves storing, indexing, and serving complex fences. Each fence is represented as a polygon along with some metadata. The key function of the API is finding all fences a certain point is in.
This process is not actually straightforward and, depending on the complexity of the fences, can include some intense calculations. That is why Google App Engine is used for the API. The following components of Google App Engine are used in the module:
The code itself and the data are hosted in Google Cloud. Depending on the complexity of the fences, more or fewer resources are needed for the calculation. For most e-commerce-related cases, the basic configuration is enough.
These are the interfaces in the API:
- Adding fences to the Google Datastore collection.
- Listing all fences from Google Datastore
- Getting fence metadata by its ID
- Building and storing the index in memcache
- Finding all fences a certain point is in
- Finding a fence that intersects with a polyline.
- Finding a fence that intersects with a polygon.
For the last three operations, the Java Topology Suite is used. Memcached is used for fast querying in a spatial index. The solution uses an STR-Tree, part of JTS, which is stored as a Java object in memcache for fast access.
The API is based on REST. Requests and responses are JSON.
Video
© Rauf Aliev, October 2016