Implementing Nearby Search with Redis GEO in a NestJS Backend

This tutorial demonstrates how to use Redis' GEO data structure together with Docker, RedisInsight, and a NestJS backend to store location data, perform distance calculations and radius searches, and finally display nearby points on a web map using AMap and Axios.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Implementing Nearby Search with Redis GEO in a NestJS Backend

The article explains that many location‑based features such as ride‑hailing, shared power banks, hotels, and nearby people are implemented using Redis GEO, which stores geographic coordinates and supports distance and radius queries.

First, Docker Desktop is used to run a Redis container, exposing port 6379 and mounting a data volume. RedisInsight is then installed to visualize keys.

Next, a NestJS project is created ( npm i -g @nestjs/cli && nest new nearby-search) and the redis client library is added. A RedisModule provides a REDIS_CLIENT provider that connects to the local Redis instance, and a RedisService wraps geoAdd, geoPos, geoList, and geoSearch methods using the Redis client.

The AppController injects RedisService and defines routes: GET /addPos?name=...&longitude=...&latitude=... – adds a point. GET /allPos – lists all points. GET /pos?name=... – retrieves a point's coordinates. GET /nearbySearch?longitude=...&latitude=...&radius=... – searches points within a radius (km).

After adding several sample locations (e.g., Tiananmen, Cultural Palace), the distance between points is verified with GEODIST, and radius queries demonstrate how the search radius affects results.

For the frontend, a static public/index.html is served, loading the AMap JavaScript SDK. A map is initialized centered on a sample coordinate, and markers and circles are added to illustrate points.

Axios is then used to call the /nearbySearch API, retrieve nearby points, and dynamically create AMap markers for each result, drawing a circle representing the search radius.

The final result shows a working end‑to‑end system where a NestJS backend stores location data in Redis GEO, provides APIs for adding and querying points, and a web page visualizes nearby points on a map.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDockerredisAmapNestJSGEOLocation Search
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.