Redis根据中心点坐标和半径筛选符合的数据

目录

1.启动Redis​编辑

2.导入maven依赖

3.添加redis配置

4.编写RedisService

5.使用

6.验证


1.启动Redis

2.导入maven依赖

java 复制代码
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3.添加redis配置

java 复制代码
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接超时时间(毫秒)
spring.redis.timeout=30000

4.编写RedisService

java 复制代码
package com.zsp.quartz.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 存入redis
    public void setValue(String key, String value) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }
    // 从redis取值
    public String getValue(String key) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }
    // 存入redis带过期时间
    public void setValue(String key, String value, long expirationTimeInSeconds) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, expirationTimeInSeconds, TimeUnit.SECONDS);
    }

    /**
     * 添加地理位置信息,附加名字
      * @param key 存入redis的key
     * @param longitude 经度
     * @param latitude  纬度
     * @param member   附加值 这里附加了名字
     */ 
    public void addLocation(String key, double longitude, double latitude, String member) {
        Point point = new Point(longitude, latitude);
        GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
        geoOps.add(key, point, member);
    }

    /**
     * 获取地理位置信息
     * @param key 存入redis的key
     * @param longitude 中心点经度
     * @param latitude  中心点纬度
     * @param radius 筛选半径
     * @return
     */
    public GeoResults<RedisGeoCommands.GeoLocation<String>> getLocationsInRadius(String key, double longitude, double latitude, double radius) {
        Point point = new Point(longitude, latitude);
        Distance distance = new Distance(radius);
        Circle circle = new Circle(point, distance);
        GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
        return geoOps.radius(key, circle);
    }
}

5.使用

java 复制代码
 @Autowired
    RedisService redisService;
    @Test
    void testForGeo(){
        redisService.addLocation("locations", 13.361389, 38.115556, "Paris");
        redisService.addLocation("locations", 13.4125, 39.523333, "Berlin");
        redisService.addLocation("locations", -0.127758, 51.507351, "London");

        GeoResults<RedisGeoCommands.GeoLocation<String>> locations = redisService.getLocationsInRadius("locations", 13.361389, 38.115556, 500000);
        for (GeoResult<RedisGeoCommands.GeoLocation<String>> location : locations) {
            System.out.println(location.getContent().getName());
        }
    }

6.验证

筛选半径单位:米,先存进去,再根据key和筛选半径取出符合条件的数据

相关推荐
ZhengEnCi1 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
吴名氏.2 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
kyriewen2 小时前
我给 61 个前端库排了两次名,最多差了 46 位
前端·javascript·程序员
JellyfishMIX2 小时前
AgentScope-Java 核心机制(内循环) ReAct loop 源码分析
java·后端·agent
亲亲小宝宝鸭3 小时前
Cesium启动:TLE数据是如何变成轨迹线的
前端·cesium
阿里云云原生3 小时前
对话即构建,在 AgentCore 构建 Agent 就是这么简单
前端·agent
夕水3 小时前
我给我的诗写了个后台,顺手写了 一个组件库ew-solid-ui
前端·javascript·typescript
Rain的Java大神之路4 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
狗头大军之江苏分军4 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬4 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust