redis内部使用的是 zset 数据结构存储,如下
import cn.huawei.VideoApplication;
import cn.huawei.domain.Jingqu;
import cn.huawei.service.JingquService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;@SpringBootTest(classes = VideoApplication.class)
public class Geo_test {@Autowiredprivate JingquService jingquService;@Autowiredprivate StringRedisTemplate stringRedisTemplate;public static final String REDIS_KEY="funplace";/*** 添加一个或多个地理位置到指定的Geo键中*/@Testpublic void test_0() {//mybatis-plus 查询出所有的景区数据List<Jingqu> list = jingquService.list();Stream<RedisGeoCommands.GeoLocation<String>> geoLocationStream = list.stream().map(jq -> {return new RedisGeoCommands.GeoLocation<String>(jq.getName(),new Point(Double.valueOf(jq.getLongitude()), Double.valueOf(jq.getLatitude())));});List<RedisGeoCommands.GeoLocation<String>> jingquList = geoLocationStream.collect(Collectors.toList());Long resultCount = stringRedisTemplate.opsForGeo().add(REDIS_KEY, jingquList);System.out.println(resultCount);}/*** 通过经纬度查找附近的景区,附近的5个景区,从近到远*/@Testvoid test_1() {Circle circle = new Circle(116.404272, 39.906277, Metrics.KILOMETERS.getMultiplier());RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5);GeoResults<RedisGeoCommands.GeoLocation<String>> byxy = stringRedisTemplate.opsForGeo().radius(REDIS_KEY, circle, args);byxy.forEach(geoResult -> {System.out.println(geoResult);System.out.println();});}/*** 通过 某个key,地方 查找附近3km的10个景区*/@Testvoid test_2(){String k="天趣园";RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(10);GeoResults<RedisGeoCommands.GeoLocation<String>> radius = stringRedisTemplate.opsForGeo().radius(REDIS_KEY, k, new Distance(3, Metrics.KILOMETERS), args);radius.forEach(geoResult->{System.out.println(geoResult);System.out.println();});}/*** 计算两个成员之间的距离(默认以米为单位),距离单位可以是m、km、ml、ft,分别代表米、千米、英里和尺*/@Testvoid test_3(){Distance distance = stringRedisTemplate.opsForGeo().distance(REDIS_KEY, "太子林", "五色门", RedisGeoCommands.DistanceUnit.KILOMETERS);System.out.println("distance = " + distance.getValue());}/*** 获取指定成员的地理位置 坐标*/@Testvoid test_4(){//注意:GeoHash对二维经纬度坐标进行一维映射是有损的,通过映射再还原回的经纬度坐标和原始输入的经纬度坐标存在一定的误差。List<Point> position = stringRedisTemplate.opsForGeo().position(REDIS_KEY, "太子林", "五色门");position.forEach(p->{System.out.println(p);System.out.println();});}/*** 获取元素经纬度坐标经过geohash算法生成的base32编码值*/@Testvoid test_5(){List<String> hash = stringRedisTemplate.opsForGeo().hash(REDIS_KEY, "太子林", "五色门");System.out.println(hash);}}