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