Store locator
Many websites provide a search function to help users find nearby locations such as retail stores. Typically, a store locator service is implemented on-premise or at the cloud origin, to search a database to find physical locations near the user. You can improve the performance of this search by executing it in a serverless function at the Edge with EdgeWorkers.
📘 Go to the Akamai Edge Compute demo site to see this example in action and to view the underlying code.
Before you begin
Finding the nearest store requires three key pieces of information:
- The user location expressed as latitude and longitude
- Store locations, including latitude and longitude
- A function to find the closest locations to the user
The first two items are relatively simple. However, writing an efficient function to find the closest locations to the user is deceptively complex. While it sounds easy to calculate the distance of each location from the user and return those with the shortest distance, iterating through hundreds of locations and recalculating distances on every user request is not an efficient use of computing resources.
Instead of determining the distance from every user to every store, you can use spatial indexing to arrange location data into a structure that supports efficient searching. While it’s possible to write your own spatial search algorithms, the process is time consuming and requires an in depth understanding of the underlying data structures.
Fortunately, you don’t need to reinvent the wheel. There is a large library of open-source JavaScript modules available through npm. Dependencies are then packaged into the EdgeWorker module with rollup. In this article, we will show you how to use npm and rollup to include geokdbush and kdbush, two JavaScript libraries that power fast spatial indexing, and search.
1. Install and quickstart
The result of this tutorial is available on GitHub in the EdgeWorkers Examples Repository.
Follow these steps to view the sample EdgeWorkers project and build the bundle:
-
Clone the source code from the Akamai EdgeWorkers Examples Repository.
git clone https://github.com/akamai/edgeworkers-examples.git -
Change the directory into the store locator path and install dependencies from the npm.
- Build the EdgeWorkers code bundle with npm.
-
Locate the bundle at
dist/storelocator.tgz.Learn more about the store locator microservice.
2. Determine user location
To locate stores that are close to the site visitor, your web application needs to determine where the user is located. The browser’s geolocation API provides a precise location if the user accepts permissions to access it. For consumers who do not, IP-based geolocation can approximate their location, as described in the Geo-Based Redirect example.
3. Retrieve store locations
The next set of information required is a list of store locations. Each location must include the latitude, longitude, and any other data necessary for the store locator service to deliver accurate results. You can then encode the data in a JSON object and deploy it with your EdgeWorkers code.
- Query data from the OpenStreetMap Overpass API to generate a list of locations. For this example, use Overpass Turbo to search Walmart stores in the U.S. with this query:
- Save the results to a JSON file called
data/locations.json.
You can find an example in the GitHub repo.
4. Find the closest stores
Once you know the locations of the users and stores, the final step is to find the closest stores. As mentioned earlier, looping through every store and calculating the distance to each is not going to be fast.
Instead, you can use a spatial index to arrange location data into a structure that supports efficient searching will speed results. To avoid the time and effort of writing the spatial indexing and search logic, we recommend you use geokdbush. Geokdbush is an open source JavaScript library that indexes geographic data in a k-d tree and implements a fast nearest-neighbor search. For more details on spatial indexing and search algorithms, visit the R-tree wiki.
5. Manage dependencies
-
Execute
npm initin an empty directory to create an npm project. -
Change the entry point to
main.js.This creates a
package.jsonfile that contains the project metadata.
- Install dependency modules.
These commands download the kdbush and geokdbush modules into the node_modules directory and update the dependencies in the package.json file.
6. Add location data
Save the results from the Overpass search to a JSON file, for example,data/locations.json. In the next step you’ll package the data into an ES module using rollup.
A fragment of the location data is shown here. The full file is available on GitHub.
7. Write and bundle EdgeWorkers code
This code indexes the location data upon initialization of the EdgeWorkers module. On each HTTP request, it will read the latitude and longitude from incoming query string parameters, search the indexed data for the nearest two stores, and respond with the result as a JSON object.
- Create a JavaScript file called
main.jsthat contains this code.
- Create a
bundle.jsonfile.
This is a manifest that contains the version and description of the EdgeWorker.
- Bundle the module.
This command installs rollup into the project as a development dependency.
- Install rollup plug-in modules.
- Create a
rollup.config.jsfile that contains the configuration options for rollup.
-
Execute
rollup -cfrom the command line to bundle the EdgeWorker module and associated files into thedist/workdirectory. -
Add a build script in the
package.jsonfile to execute rollup and build the.tgzfile.
- Issue this command to build the EdgeWorkers code bundle.
Once you’ve created the .tgz, you can upload and deploy it. Learn more about how to Manage EdgeWorkers.