实战篇--附近店铺

附近店铺

GEO数据结构

img

基本使用

# 添加三个坐标到geo中
GEOADD g1 116.378248 39.865275 bjn 116.42803 39.903738 bjz 116.322287  39.893729 bjx
# 返回两点之间的距离(默认是米)
GEODIST g1 bjn bjx
# 返回两点之间的距离(以km返回)
GEODIST g1 bjn bjx km
# 搜索以某个点附近10km内的点带上距离
GEORADIUS g1 116.397904 39.909005 10 km withdist

搜索附近店铺

img

导入数据

@Test
    void method1() {
        // 查数据库将所有店铺信息查出来
        List<Shop> shops = iShopService.list();
        // 通过类型分组
        Map<Long, List<Shop>> map = shops.stream().collect(Collectors.groupingBy(Shop::getTypeId));
        // 遍历所有map
        for(Map.Entry<Long,List<Shop>> shop : map.entrySet()) {
            拿到每个类型下的所有店铺集合
            List<Shop> shopList = shop.getValue();
            // 根据店铺类型来创建key
            Long typeId = shop.getKey();
            String key = "shop:geo:" + typeId;
            // 创建redis中的数据结构
            List<RedisGeoCommands.GeoLocation<String>> geoList = new ArrayList<>(shopList.size());
            // 遍历所有店铺
            for (Shop s : shopList) {
                // 将店铺信息的id作为member放入redis集合
                geoList.add(new RedisGeoCommands.GeoLocation<>(s.getId().toString(),new Point(s.getX(),s.getY())));
            }
            // 插入操作
            stringRedisTemplate.opsForGeo().add(key,geoList);
        }
    }

实现附近的店铺

    @Override
    public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
        if (x == null || y == null) {
            Page<Shop> page = query()
                    .eq("type_id", typeId).page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
            return Result.ok(page.getRecords());
        }
        int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;
        int end = current * SystemConstants.DEFAULT_PAGE_SIZE;
        String key = "shop:geo:" + typeId;
        GeoResults<RedisGeoCommands.GeoLocation<String>> radius = stringRedisTemplate.opsForGeo().radius(key, new Circle(new Point(x, y), new Distance(5000)), RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending().limit(end).includeDistance());
        if (radius == null) {
            return Result.ok();
        }
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = radius.getContent();
        if (content.size() <= from) {
            return Result.ok();
        }
        List<Shop> shops = content.stream().skip(from).map(r -> {
            Shop shop = this.getById(Integer.parseInt(r.getContent().getName()));
            shop.setDistance(r.getDistance().getValue());
            return shop;
        }
        ).collect(Collectors.toList());
        return Result.ok(shops);
    }
        ).collect(Collectors.toList());
        return Result.ok(shops);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值