Redsi通过geo计算距离
生活随笔
收集整理的這篇文章主要介紹了
Redsi通过geo计算距离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一. 前言
前段時間,小熙趕項目比較忙。趁此機會記錄下遇到的后端距離計算實現,app端會有實時的經緯度回傳到Redis中,PC端和H5需要實時查看位置和距離,所以想下Redis是否支持此類計算。
二. Redis的geo介紹
版本支持:
redis在3.2版本中開始支持geo功能命令介紹:
(1) geoadd 添加地址
(2)geodist 計算距離(默認是米,可以指定單位千米)
sms-center:5>geodist cityLocationGeo 北京 上海1067597.9668sms-center:5>geodist cityLocationGeo 北京 上海 km1067.5980(3)geohash 獲取地址的hash值(可用于判斷是否存在)
sms-center:5>geohash cityLocationGeo 上海 北京wtw3sjt9vg0wx4g0b7xrt0(4)geopos 獲取地理位置(經緯度)
sms-center:5>geopos cityLocationGeo 北京1) "116.40528291463851929"2) "39.9049884229125027"(5)zrem 刪除某個地址(redis中沒有geodel命令)
sms-center:5>zrem cityLocationGeo 北京1其他更多詳細命令請查詢文檔
三. redisTemplate 使用 geo
這里的storeKey和memberKey是兩個地理位置的keyName,可任意替換。
添加geo
/*** 向redis中添加geo計算數據** @param prefixName* @param caseDetail* @param latitude* @param longitude* @param name* @return*/private String addGeoData(String prefixName, CaseDetail caseDetail, String latitude, String longitude, String name) {String geoKey = prefixName + "_" + caseDetail.getStoreNo() + "_" + caseDetail.getCaseNo() + "_LatitudeAndLongitude";Point point = new Point(Double.valueOf(longitude), Double.valueOf(latitude));redisTemplate.opsForGeo().add(geoKey, point, name);redisTemplate.expire(geoKey, 30, TimeUnit.SECONDS);return geoKey;}計算距離
// 計算距離Distance distance = redisTemplate.boundGeoOps(geoKey).distance(storeKey, memberKey, RedisGeoCommands.DistanceUnit.KILOMETERS);double value = distance.getValue();獲取hash地址
List hash = redisTemplate.boundGeoOps(geoKey).hash(storeKey);獲取地理位置(經緯度)
List<Point> position = (List<Point>) redisTemplate.boundGeoOps(geoKey).position(memberKey).stream().filter(Objects::nonNull).collect(Collectors.toList());刪除某個地理位置
Long remove = redisTemplate.boundGeoOps(geoKey).remove(storeKey);四. 后語
基本開發以上的可以了,如果想了解更多,可以查看文檔。
總結
以上是生活随笔為你收集整理的Redsi通过geo计算距离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (入门)python的基本输入和输出
- 下一篇: 【我的OpenGL学习进阶之旅】关于Op