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和筛选半径取出符合条件的数据

相关推荐
逆境不可逃1 小时前
一篇速通互联网架构的不断升级过程:从单机到云原生
java·elasticsearch·搜索引擎·云原生·架构
scott.cgi3 小时前
Unity直接编译Java文件作为插件,导致失败的两个打包设置
java·unity·unity调用java·unity的java文件·unity的android插件·unity调用android·unity加载java代码
KaMeidebaby6 小时前
卡梅德生物技术快报|骆驼纳米抗体:从原核表达、高通量测序到分子对接全流程实现
前端·数据库·其他·百度·新浪微博
澈2077 小时前
C++并查集:高效解决连通性问题
java·c++·算法
子兮曰8 小时前
Node.js v26.1.0 深度解读:FFI、后量子密码与调试器的进化
前端·后端·node.js
测试员周周8 小时前
【Appium 系列】第06节-页面对象实现 — LoginPage 实战
开发语言·前端·人工智能·python·功能测试·appium·测试用例
2401_873479408 小时前
运营活动被薅羊毛怎么防?用IP查询+设备指纹联动封堵漏洞
java·网络·tcp/ip·github
ShiJiuD6668889998 小时前
大事件板块一
java
摇滚侠9 小时前
@Autowired 和 @Resource 的区别
java·开发语言
SeaTunnel9 小时前
(八)收官篇 | 数据平台最后一公里:数据集成开发设计与上线治理实战
java·大数据·开发语言·白鲸开源