redis计算经纬度距离
使用场景:
1.用户查询附近的酒店
2.游戏查看附件一起玩的人
3.交友app查看附件的人
注意:redis需要从3.2版本开始才支持
import lombok.NonNull;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.*;/*** redis经纬度计算工具类** @author compass* @date 2023-02-12* @since 1.0**/
@Component
public class GeoUtils {@Resourceprivate RedisTemplate<String, Object> redisTemplate;public static final String GEO_CACHE_KEY = "geo_cache_key";/*** 将指定的地理空间位置(纬度、经度、名称)添加到指定的key中。** @param cacheKey 缓存key* @param point 经纬度位置* @param certId 成员标识* @return boolean* @author compass* @date 2023/2/12 23:39* @since 1.0.0**/public boolean geoAdd(String cacheKey, Point point, String certId) {Long result = redisTemplate.opsForGeo().add(cacheKey, point, certId);return result != null && result > 0;}/*** 将经纬度信息添加到redis中** @param cacheKey 缓存主键* @param params params key为成员标识,value为经纬度信息* @return boolean* @author compass* @date 2023/2/13 9:57* @since 1.0.0**/public boolean geoAddAll(String cacheKey, Map<Object, Point> params) {GeoOperations<String, Object> ops = redisTemplate.opsForGeo();Long result = ops.add(cacheKey, params);return result != null && result > 0;}/*** 计算两个人之间的距离** @param cacheKey 缓存主键* @param selfCertId 第一个标识* @param otherCertId 第二个标识* @return double* @author compass* @date 2023/2/13 10:23* @since 1.0.0**/public double distanceBetween(String cacheKey, String selfCertId, String otherCertId) {GeoOperations<String, Object> geoOperations = redisTemplate.opsForGeo();Distance distance = geoOperations.distance(cacheKey, selfCertId, otherCertId);if (distance != null) {return distance.getValue();}return -1;}/*** 查询距离某个人指定范围内的人,包括距离多少米** @param cacheKey 缓存主键* @param certId 目标标识* @param distance 距离(米)* @param limit 距离单位* @param limit 限制个数* @return java.util.Map<java.lang.String, java.lang.Double>* @author compass* @date 2023/2/13 9:59* @since 1.0.0**/public Map<String, Double> distanceInclude(String cacheKey, String certId, double distance, long limit, RedisGeoCommands.DistanceUnit distanceUnit) {RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending()// 排序.limit(limit)// 输出元素的个数.includeCoordinates();// 输出经纬度GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = redisTemplate.opsForGeo().radius(cacheKey, certId, new Distance(distance, distanceUnit), geoRadiusCommandArgs);HashMap<String, Double> resultMap = new HashMap<>();if (geoResults != null) {List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> content = geoResults.getContent();for (GeoResult<RedisGeoCommands.GeoLocation<Object>> item : content) {RedisGeoCommands.@NonNull GeoLocation<Object> itemContent = item.getContent();String otherCertId = itemContent.getName().toString();double between = distanceBetween(cacheKey, certId, otherCertId);resultMap.put(otherCertId, between);}}return resultMap;}/*** 查询距离某个人指定范围内的人,包括距离多少米* @param cacheKey 缓存主键* @param certId 目标标识* @param distance 距离(米)* @return java.util.Map<java.lang.String, java.lang.Double>* @author compass* @date 2023/2/13 11:06* @since 1.0.0**/public Map<String, Double> distanceInclude(String cacheKey, String certId, double distance) {Map<String, Double> map = new LinkedHashMap<>();GeoOperations geoOperations = redisTemplate.opsForGeo();RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = geoOperations.radius(cacheKey, certId, new Distance(distance), geoRadiusCommandArgs.includeDistance());if (geoResults != null) {Iterator<GeoResult<RedisGeoCommands.GeoLocation<String>>> iterator = geoResults.iterator();while (iterator.hasNext()) {GeoResult<RedisGeoCommands.GeoLocation<String>> geoResult = iterator.next();// 与目标点相距的距离信息Distance geoResultDistance = geoResult.getDistance();// 该点的信息RedisGeoCommands.GeoLocation<String> geoResultContent = geoResult.getContent();map.put(geoResultContent.getName(), geoResultDistance.getValue());}}return map;}/*** 获取指定目标的经纬度** @param cacheKey 缓存主键* @param certId 目标标识* @return java.util.List<org.springframework.data.geo.Point>* @author compass* @date 2023/2/13 3:06* @since 1.0.0**/public List<Point> position(String cacheKey, String certId) {return redisTemplate.opsForGeo().position(cacheKey, certId);}/*** 将指定单位从geo中异常** @param cacheKey 缓存主键* @param certIds 成员标识* @return boolean* @author compass* @date 2023/2/13 9:56* @since 1.0.0**/public boolean remove(String cacheKey, String... certIds) {Long result = redisTemplate.opsForGeo().remove(cacheKey, certIds);return result != null && result > 0;}/*** 获取地理位置的哈希值** @param cacheKey 缓存主键* @param certIds 成员标识* @return java.util.List<java.lang.String>* @author compass* @date 2023/2/13 10:22* @since 1.0.0**/public List<String> hashByCertId(String cacheKey, String... certIds) {return redisTemplate.opsForGeo().hash(cacheKey, certIds);}}
redis计算经纬度距离相关推荐
- Python使用Redis计算经纬度距离
1. 需要的库, redis. pip install redis 2. 连接Redis import redisclass RedisCtrl(object):@staticmethoddef co ...
- Oracle经纬度查询最近sql,SQL语句计算经纬度距离
二: SQL语句计算经纬度距离 SELECT id, ( 6371* acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( ...
- MySQL计算经纬度距离
MySQL计算经纬度距离 现在开发计算距离自身所在地到目的地的距离算是一个比较常见的需求,基本上都是通过两地的经纬度查询直线距离,忘记之前自己有没有写过,反正印象都是通过一定的算法来获取的. 今天开发 ...
- 使用redisTemplate-geo计算经纬度距离
简介 业务中常有需求是计算经纬度之间的距离,redis是使用较多的缓存中间件,正好有关于geo位置计算的api,可以直接拿来用. redis依赖 <dependency><group ...
- Python:计算经纬度距离
Python计算经纬度的方法: 这里介绍两种方式,其最终结果都是以[米]作为单位,虽然结果值稍微有误差,但误差不大. 1. List item 方式1: 维基百科公式(要求的是公式中的d): Pyth ...
- 使用Redis进行经纬度距离
业务需求: 客户端传入用户的经纬度,后端计算出该经纬度与指定经纬度之间的距离; 在面向百度之后,发现Redis在3.2之后推出GEO特性可以简单就解决这个需求; 为了验证其的可用性(误差不要太多),这 ...
- MYSQL创建一个function用来计算经纬度距离
2019独角兽企业重金招聘Python工程师标准>>> 店铺表有一个经度字段,一个纬度字段,用来存储所在位置.先需要根据当前位置的经度与纬度来获取附近10公里内的店铺.故需要将计算距 ...
- php计算经纬度距离,php经纬度计算距离
/** * 计算两点地理坐标之间的距离 * @param Decimal $longitude1 起点经度 * @param Decimal $latitude1 起点纬度 * @param ...
- android 经纬度工具类,计算经纬度距离工具类
public class LocationUtils { private static double EARTH_RADIUS = 6378.137; private static double ra ...
最新文章
- 漫画:什么是八皇后问题?
- 科大讯飞年报出炉,2018每天赚148万元,53%是政府补助
- 信息系统项目管理师-知识管理知识点
- Java 8 开发的 4 大顶级技巧
- OpenCV支持向量机SVM和SDG算法的实例(附完整代码)
- matlab 28m35,F28M35H52C
- 一台服务器搭建部署两个或多个Redis实例
- using 关键字有两个主要用途
- java运用ascii实现动画效果_安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果...
- 隐藏html文本节点,javascript-D3-仅显示/隐藏单击节点的文本
- 零售连锁专卖信息化解决方案简介之一
- vue 子组件给父组件传值
- 练手|常见近30种NLP任务的练手项目
- 点云数据增强及预处理
- 【深度学习】写诗机器人tensorflow实现
- less面试_想获得理想工作?面试时千万不要说这七句话
- MarkdownPad2 使用教程
- C#使用公共语言拓展(CLE)调用Python3(使用TensorFlow训练的模型)
- 个人日记-学习究竟是什么读后感4-2020/7/19
- 个人博客作业三:英语学习APP的案例分析