JAVA使用Redis获取周围商铺

GEO

@Data
public class Geo<T> {

    private T object;

    private double distance;
}
​

@RequestMapping("/api/location_near_shop/Li_member_near_shop")
public class Li_member_near_shopController {

    @Autowired
    private Li_member_idcard_auditService li_member_idcard_auditService;

    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisGeo redisGeo;
    @Component
    public class RedisGeo<T> {

        @Resource(name = "redisTemplate")
        private RedisTemplate redisTemplate;

        public void setGeo(String key, double longitude, double latitude, T object) {
            redisTemplate.opsForGeo().add(key, new Point(longitude, latitude), object);
        }

        public void removeGeo(String key, T object) {
            redisTemplate.opsForGeo().remove(key, object);
        }

        public List<Geo<T>> getNearbyByGeo(String key, double longitude, double latitude, int distance, int limit) {
            List<Geo<T>> geos = new ArrayList<>();
            BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps(key);
            Point point = new Point(longitude, latitude);
            Circle within = new Circle(point, distance);
            RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
            geoRadiusArgs = geoRadiusArgs.includeDistance();
            geoRadiusArgs.limit(limit);
            geoRadiusArgs.sortAscending();
            GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = boundGeoOperations.radius(within, geoRadiusArgs);
            List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> geoResultList = geoResults.getContent();
            Geo geo;
            for (GeoResult<RedisGeoCommands.GeoLocation<Object>> geoResult : geoResultList) {
                geo = new Geo();
                geo.setObject(geoResult.getContent());
                geo.setDistance(geoResult.getDistance().getValue());
                geos.add(geo);
            }
            return geos;
        }
    }



    @Autowired
    private StoreService storeService;

//将商铺同步到redis
    @PostMapping(value = "/insertAllShopRedis")
    public ResultMessage<Object> insertAllShopRedis() {
        List<Store> allStoreList = storeService.list();
        for (Store store : allStoreList) {
            StoreLocBean storeLocBean=new StoreLocBean();
            String storeCenter = store.getStoreCenter();
            if(storeCenter==null){
                return ResultUtil.error(400,"没有经纬度");
            }
            String[] split = storeCenter.split(",");
            BeanUtil.copyProperties(store,storeLocBean);
            Double loc1 = Double.valueOf(split[1]);
            Double loc2 = Double.valueOf(split[0]);
            System.out.println(loc1);
            System.out.println(loc2);
            if(loc1>loc2){
                storeLocBean.setLongitude(String.valueOf(loc1));
                storeLocBean.setLatitude(String.valueOf(loc2));
                redisGeo.setGeo("store",loc1 ,loc2, storeLocBean);
            }else{
                storeLocBean.setLongitude(String.valueOf(loc2));
                storeLocBean.setLatitude(String.valueOf(loc1));
                redisGeo.setGeo("store",loc2 ,loc1, storeLocBean);
            }
            System.out.println("______________________"+storeLocBean);
        }
        return ResultUtil.success();
    }

//根据经纬度获取周围商铺

    @GetMapping("/getNearShop")
    public ResultMessage<Object> getNearShop(Double longitude0 ,Double latitude0 ) {
        int distance = 20000;
        int limit=20;
        List<Geo<StoreLocBean>> list = redisGeo.getNearbyByGeo("store", longitude0, latitude0, distance,limit);

       if (CollectionUtils.isNotEmpty(list)) {
            for (Geo<StoreLocBean> geo : list) {
                System.out.println((geo.getObject()) + "----------" + geo.getDistance());
            }
        }
        Page<Geo<StoreLocBean>> geoPage = listToPage(list, 10, 1);
        return ResultUtil.data(geoPage);


    }

//将list列表转为分页列表
    private <T> Page<T> listToPage(List<T> dataList, Integer pageSize, Integer pageNumber) {
        Page<T> result = new Page<T>();
        List<T> monitors = new ArrayList<T>();
        if (dataList != null && dataList.size() > 0) {
            int currentIndex = pageNumber > 1 ? (pageNumber - 1) * pageSize : 0;
            for (int i = 0; i < pageSize && i < dataList.size() - currentIndex; i++) {
                T data = dataList.get(currentIndex + i);
                monitors.add(data);
            }
            result.setRecords(monitors);
            result.setTotal(dataList.size());
            result.setCurrent(pageNumber);
            result.setSize(pageSize);
        }
        return result;
    }
}

​

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值