博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot集成redis的LBS功能
阅读量:5882 次
发布时间:2019-06-19

本文共 5697 字,大约阅读时间需要 18 分钟。

下面的代码实现了添加经纬度数据 和 搜索经纬度数据的功能:

import java.util.List;import com.longge.goods.dto.BuildingDto;import com.longge.goods.dto.BuildingLbsDto;/** * LBS相关的服务 * @author yangzhilong * */public interface LbsService {    /**     * 新增或者修改楼宇的经纬度     * @param buildDto     */    void saveBuildingLonAndLat(BuildingDto buildDto);        /**     * 查询指定经纬度附近的楼宇     * @param lon 经度     * @param lat 纬度     * @param limit 记录数     * @return     */    List
listNearbyBuilding(double lon, double lat, int limit); }

 

1 import java.util.ArrayList;  2 import java.util.HashMap;  3 import java.util.List;  4 import java.util.Map;  5   6 import javax.annotation.Resource;  7   8 import org.apache.commons.lang3.StringUtils;  9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Value; 11 import org.springframework.data.geo.Circle; 12 import org.springframework.data.geo.Distance; 13 import org.springframework.data.geo.GeoResults; 14 import org.springframework.data.geo.Metrics; 15 import org.springframework.data.geo.Point; 16 import org.springframework.data.redis.connection.RedisGeoCommands; 17 import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation; 18 import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs; 19 import org.springframework.data.redis.core.GeoOperations; 20 import org.springframework.data.redis.core.StringRedisTemplate; 21 import org.springframework.stereotype.Service; 22 import org.springframework.util.CollectionUtils; 23  24 import com.alibaba.fastjson.JSONObject; 25 import com.longge.core.util.BeanMapper; 26 import com.longge.goods.api.BuildingService; 27 import com.longge.goods.api.LbsService; 28 import com.longge.goods.constants.BuildingConstant; 29 import com.longge.goods.dto.BuildingDto; 30 import com.longge.goods.dto.BuildingLbsDto; 31 import com.longge.goods.util.GoodsRedisKeyUtils; 32  33 import lombok.extern.slf4j.Slf4j; 34  35 @Service 36 @Slf4j 37 public class LbsServiceImpl implements LbsService { 38     @Autowired 39     private BuildingService buildingService; 40     // 搜索范围 41     @Value("${lbs.distance:3}") 42     private Double lbsDistance; 43     @Resource 44     private StringRedisTemplate redisTemplate; 45  46     @Override 47     public void saveBuildingLonAndLat(BuildingDto buildDto) { 48         log.info("开始处理楼宇的经纬度数据,楼宇:{}", JSONObject.toJSONString(buildDto)); 49         GeoOperations
ops = redisTemplate.opsForGeo(); 50 String member = String.valueOf(buildDto.getId()); 51 // 查询楼宇的geo数据是否存在 52 List
list = ops.hash(GoodsRedisKeyUtils.getBuildingLbsKey(), member); 53 if (!CollectionUtils.isEmpty(list)) { 54 log.info("该楼宇数据存在,先进行删除操作"); 55 // 存在,先删除,后新增 56 ops.remove(GoodsRedisKeyUtils.getBuildingLbsKey(), member); 57 } 58 ops.add(GoodsRedisKeyUtils.getBuildingLbsKey(), 59 new RedisGeoCommands.GeoLocation
(member, new Point(buildDto.getLon(), buildDto.getLat()))); 60 log.info("楼宇经纬度信息处理完成"); 61 } 62 63 @Override 64 public List
listNearbyBuilding(double lon, double lat, int limit) { 65 log.info("查询LBS楼宇信息,经度:{},纬度:{},条数:{}", lon, lat, limit); 66 GeoOperations
ops = redisTemplate.opsForGeo(); 67 68 // 获取redis里全局的配置,实现参数动态化 69 String redisLbsDistance = (String)RedisCache.hGet("config:global", "lbsDistance"); 70 if(StringUtils.isNotBlank(redisLbsDistance)) { 71 lbsDistance = Double.valueOf(redisLbsDistance); 72 } 73 74 Point center = new Point(lon, lat); 75 Distance radius = new Distance(lbsDistance, Metrics.KILOMETERS); 76 Circle within = new Circle(center, radius); 77 // order by 距离 limit 20 ,同时返回距离中心点的距离 78 GeoRadiusCommandArgs args = GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().limit(limit).sortAscending(); 79 80 GeoResults
> result = ops.radius(GoodsRedisKeyUtils.getBuildingLbsKey(), within, args); 81 log.info("查询redis后的经纬度数据:{}", JSONObject.toJSONString(result)); 82 if(null != result) { 83 Map
data = new HashMap<>(); 84 List
buildings = new ArrayList<>(); 85 86 result.forEach(item -> { 87 data.put(Long.valueOf(item.getContent().getName()), item.getDistance().getValue()); 88 buildings.add(Long.valueOf(item.getContent().getName())); 89 }); 90 if(!CollectionUtils.isEmpty(buildings)) { 91 List
buildingList = buildingService.getBuildList(buildings); 92 if(!CollectionUtils.isEmpty(buildingList)) { 93 List
resp = new ArrayList<>(); 94 buildingList.stream().forEach(item -> { 95 BuildingLbsDto dto = BeanMapper.map(item, BuildingLbsDto.class); 96 if(BuildingConstant.BuildingStatusEnum.OPENED.getValue() == dto.getStatus()) { 97 dto.setDistance(data.get(item.getId())); 98 resp.add(dto); 99 }100 });101 log.info("查询LBS楼宇信息的结果:{}", JSONObject.toJSONString(resp));102 return resp;103 }104 }105 }106 log.warn("未查询LBS楼宇信息");107 return null;108 }109 }
import java.util.Date;/** * 楼宇信息dto * */@Datapublic class BuildingDto {    private Long id;    private Short status;    /**     * 纬度     */    private Double lat;    /**     * 经度     */    private Double lon;}
import java.io.Serializable;import lombok.Getter;import lombok.Setter;/** * 包含距离定位点距离的楼宇dto * @author yangzhilong * */@Getter@Setterpublic class BuildingLbsDto extends BuildingDto implements Serializable{    /**     *      */    private static final long serialVersionUID = 3224249189169148012L;    // 距离,单位:千米    private Double distance;}

 

转载地址:http://pmpix.baihongyu.com/

你可能感兴趣的文章
form组件
查看>>
List的深度copy和浅度拷贝
查看>>
c语言 变量的存储类别以及对应的内存分配?
查看>>
CCF认证历年试题集
查看>>
九章算术卷第九 句股
查看>>
从更高点看软件开发的侧重点
查看>>
01.变量和字符编码
查看>>
遗传算法求系统组合
查看>>
股指的趋势持续研究(Hurst指数)
查看>>
前端上传图片 base64转二进制上传
查看>>
结构体
查看>>
ROS主题发布订阅
查看>>
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
查看>>
python基础,python第二课
查看>>
RabbitMQ~一些术语和最消息的生产
查看>>
Redis学习笔记~把redis放在DATA层,作为一种数据源,我认为更合理,也更符合我的面向对象原则...
查看>>
vue 路由拦截、axios请求拦截
查看>>
http post get
查看>>
高度自适应的bug
查看>>
1046 Shortest Distance
查看>>