日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Redsi通过geo计算距离

發布時間:2024/1/1 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redsi通过geo计算距离 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一. 前言

前段時間,小熙趕項目比較忙。趁此機會記錄下遇到的后端距離計算實現,app端會有實時的經緯度回傳到Redis中,PC端和H5需要實時查看位置和距離,所以想下Redis是否支持此類計算。

二. Redis的geo介紹

  • 版本支持:

    redis在3.2版本中開始支持geo功能
  • 命令介紹:
    (1) geoadd 添加地址

    sms-center:5>geoadd cityLocationGeo 116.405285 39.904989 北京1sms-center:5>geoadd cityLocationGeo 121.472644 31.231706 上海1

    (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计算距离的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。