当前位置: 首页 > news >正文

38.附近商户实现

geo数据结构geolocation,地理坐标。

存储地理坐标信息

根据经纬度检索数据。

[root@localhost ~]# redis-cli
127.0.0.1:6379> geoadd g1 116.378248 39.865275 bjn 116.42803 39.903738 bjz 116.322287 39.893729 bjx
(integer) 3
127.0.0.1:6379>

获取北京南到北京西的距离,默认单位为米,指定单位km为千米。

返回北京站的坐标

返回北京站坐标的hash值

score存的是店铺经纬度坐标。这里的值可以代表和转化为真正的经纬度。

key 存入店铺类型type id,将同类型的数据放到一起。

value存入店铺的id.

修改pom文件

<!--redis springDataRedis2.3.9 并不支持redis6.2提供的GEOSearch命令,因此需要修改pom文件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></exclusion><exclusion><artifactId>lettuce-core</artifactId><groupId>io.lettuce</groupId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>2.6.2</version></dependency><dependency><artifactId>lettuce-core</artifactId><groupId>io.lettuce</groupId><version>6.1.6.RELEASE</version></dependency>

向redis中添加数据

 @Testpublic void loadShopData() {//1.查询店铺信息List<Shop> shops = shopService.list();//2.按照店铺类型分组,typeId一致的放入一个集合Map<Long, List<Shop>> typeIdShops = shops.stream().collect(Collectors.groupingBy(Shop::getTypeId));//3.分批完成写入redisfor (Map.Entry<Long, List<Shop>> entry : typeIdShops.entrySet()) {Long typeId = entry.getKey();String key = "shop:geo:" + typeId;List<Shop> shopList = entry.getValue();List<RedisGeoCommands.GeoLocation<String>> stringGeoLocation = new ArrayList<>(typeIdShops.size());//写入redis geoadd key jd wd memberfor (Shop shop : shopList) {//一个一个添加
//                stringRedisTemplate.opsForGeo().add(key,
//                        new Point(shop.getY(), shop.getX()),
//                        shop.getId().toString());//批量组装数据RedisGeoCommands.GeoLocation<String> geoLocation =new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getY(), shop.getX()));stringGeoLocation.add(geoLocation);}//批量写入redisstringRedisTemplate.opsForGeo().add(key, stringGeoLocation);}}
 @GetMapping("/of/type")public ApiResponse queryShopByType(@RequestParam("typeId") Integer typeId,@RequestParam(value = "current", defaultValue = "1") Integer current,@RequestParam(value = "x", required = false) Double x,@RequestParam(value = "y", required = false) Double y) {List<Shop> shops = shopService.queryShopByType(typeId, current, x, y);return ApiResponse.success(shops);}
@Overridepublic List<Shop> queryShopByType(Integer typeId, Integer current, Double x, Double y) {int pageSize = 5;//1.判断是否需要根据坐标查询if(x == null || y == null) {Page<Shop> page = query().eq("type_id", typeId).page(new Page<>(current, pageSize));return page.getRecords();}//2.计算分页参数int from = (current -1) * pageSize;int end = current * pageSize;//3.查询redis,按照距离排序、分页。结果:shopId,distance// geosearch key fromlonlat x y byradius 10 km with distString key = "shop:geo:" + typeId;GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo().search(key,GeoReference.fromCoordinate(x, y),//默认单位米,寻找五千米范围内的店铺new Distance(5000),//返回包含距离RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance()//默认从地0-end条的数据记录.limit(end));//4.解析出idif(results == null) {return new ArrayList<>();}List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = results.getContent();if(content.size() <= from) {//说明没有下一页了,直接返回空集合return new ArrayList<>();}//截取从from-end部分的数据List<Long> ids = new ArrayList<>(content.size());Map<String, Distance> distanceMap = new HashMap<>(content.size());content.stream().skip(from).forEach(e -> {//member值,店铺idString shopIdStr = e.getContent().getName();ids.add(Long.valueOf(shopIdStr));//获取距离Distance distance = e.getDistance();distanceMap.put(shopIdStr, distance);});//5.根据id查询shopString idStr = StrUtil.join(",", ids);List<Shop> shopList = query().in("id", ids).last("order by field (id," + idStr + ")").list();//6.返回shop集合shopList.stream().forEach(e -> e.setDistance(distanceMap.get(e.getId().toString()).getValue()));return shopList;}

http://www.dtcms.com/a/606714.html

相关文章:

  • 做网站 帮别人卖服务器成都网站设计
  • 园林景观网站源码做网站前应该先出图
  • Zookeeper 基础入门与应用场景解析
  • 雅虎做网站推广网站设计网页设计
  • cms建设网站官方网站的优势
  • c语言printf输出控制符
  • 数据库三大范式详解
  • 做精酿啤酒购买的网站竞价广告
  • JAVA国际版二手车交易二手车市场系统源码支持Android+IOS+H5+APP
  • 做电脑网站手机能显示不出来seo网络推广企业
  • 织梦网站首页打开慢怎么注册一个域名
  • 关于图的算法题总结
  • HarmonyOS:动画衔接
  • 百度收录不了网站建设网站注意实现
  • 方寸之间藏智慧:家用电器的进化与生活革新
  • 智能手机市场再次洗牌,远控何以成为数码生活新“连接器”?
  • 网站建设宣传软文范例新洲网站建设
  • 建旅游网站费用明细网站建设公司 优势
  • CSS笔记
  • 个人网站怎么建立要多少钱宣传册
  • Plaxis岩土工程全模块Python自动化建模与案例,涵盖塑性、渗流、固结、动力、稳定安全及热力TM等核心问题
  • 双人对话生成模型 MOSS 上线,支持零样本语音克隆
  • SMOTE详解
  • 16.【NXP 号令者RT1052】开发——实战-FlexPWM 输出
  • Datawhale coze-ai-assistant task2
  • NESTJS - RSA加解密
  • 自己服务器可以做网站如何做家教网站赚钱
  • 开发外贸网站开发企业官网的建设
  • 一、Rabbit MQ 初级
  • 单位网站建设费用神马搜索推广