基于redis GEO实现查找附近的门店

1.导入jar包

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

2.配置 配置文件

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=300ms
@RestController
public class GeoController {
    @Autowired
    private GeoService geoService;

    /**
     * 根据指定经纬度查询附件的店铺
     * @return
     */
    @GetMapping(value = "/test")
    public List<ShopVO> radiusByxy(){
        //这个坐标是北京王府井的位置
      //  Circle circle=new Circle(116.418017,39.914402, Metrics.KILOMETERS.getMultiplier());

        Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
        Distance distance = new Distance(2, metric);
        Circle  circle=new Circle(new Point(116.418017,39.914402),distance);
        return geoService.findRecentlyShop(circle);
    }
}


@Service
public class GeoService {
    private static final String CITY="city";
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 初始化经纬度数据
     */
    @PostConstruct
    public void initGeoData(){
        Map<String, Point> map=new HashMap<>();
        map.put("天安门",new Point(116.403963,39.915119));
        map.put("故宫",new Point(116.403414 ,39.924091));
        map.put("长城",new Point(116.024067,40.362639));
        redisTemplate.opsForGeo().add(CITY,map);
    }

    public List<ShopVO> findRecentlyShop(Circle circle) {
        /**
         *  返回50条数据
         *  includeDistance 包含距离
         *  includeCoordinates 包含经纬度
         *  sortAscending 正序排序
         *  limit 限定返回的记录数
         *
         */


            RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(50);
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius(CITY, circle, args);
        List<ShopVO> shopVOList=new ArrayList<>();
        geoResults.forEach(item->{
            RedisGeoCommands.GeoLocation<String> location = item.getContent();
            Point point = location.getPoint();
            ShopVO shopVO = ShopVO.builder().shopName(location.getName()).lng(point.getX()).lat(point.getY()).build();
            shopVOList.add(shopVO);
        });
        return shopVOList;
    }
}

/**
 * 店铺信息
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ShopVO {
    /**
     * 店铺名称
     */
    private String shopName;
    private Double lng;
    private Double lat;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值