How to Retrieve Nearby Charging Station Information Using Redis GEO
This tutorial shows how to obtain a user's current latitude and longitude via an IP API, send the coordinates to a backend service that stores charging stations in Redis GEO, and query stations within a 20‑kilometer radius, with complete front‑end and back‑end code examples.
The article demonstrates a practical implementation for fetching nearby charging station data. First, the front‑end obtains the user's current longitude and latitude by calling the https://ipapi.co/json/ endpoint with Axios, storing the values in reactive variables.
function getLocation() {
try {
const request = axios.create({
baseURL: "https://ipapi.co",
timeout: 10000
})
request.get('/json/')
.then(response => {
// current longitude and latitude
longitude.value = response.data.longitude;
latitude.value = response.data.latitude;
})
.catch(error => {
console.error("Error fetching data:", error);
});
} catch (error) {
showFailToast('获取用户当前经纬度失败');
}
}With the coordinates available, the front‑end calls a backend API to retrieve nearby stations:
function getStationList() {
const reqData = { lon: longitude.value, lat: latitude.value };
StationList(reqData)
.then(res => {
console.info(res.data);
if (res.data.code === 200000) {
stations.value = res.data.data.data;
} else {
showFailToast('没有获取到了站点数据');
}
})
.catch(() => {
showFailToast('获取站点数据失败');
});
}On the back‑end, charging station records are stored in Redis using the Redisson GEO API. Each station's longitude and latitude are added to a GEO set named station-geo:
// Add to Redis for map queries
RGeo<ChargeStationInfo> geo = redissonClient.getGeo("station-geo");
geo.add(chargeStationInfo.getLongitude().doubleValue(),
chargeStationInfo.getLatitude().doubleValue(),
chargeStationInfo);To query stations near the user, the service performs a GEO search with a 20‑kilometer radius, ordering results ascendingly and limiting the number of returned items:
List<ChargeStationInfo> chargeStationInfos = geo.search(
GeoSearchArgs.from(longitude, latitude)
.radius(20, GeoUnit.KILOMETERS)
.order(GeoOrder.ASC)
.count(querySize));The query parameters include the user's longitude and latitude, the search radius (20 km), the unit (kilometers), the sorting order, and the maximum number of records to return. The article also provides links to the full front‑end and back‑end source code and SQL scripts in an external documentation repository.
Java Backend Full-Stack
Provides technical guidance, interview coaching, and tech sharing. Follow and reply '77' to receive our self-made 'Interview Cheat Sheet' and interview resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
