使用redis實(shí)現(xiàn)附近的人功能
前言
Redis自3.2版本開(kāi)始提供了GEO(geograph)功能,支持地理位置相關(guān)操作,以實(shí)現(xiàn)諸如附近的人這類依賴于地理位置信息的功能。
工具
百度經(jīng)緯度拾取器
一、測(cè)試數(shù)據(jù)
120.70012 28.00135 溫州
120.207686 30.253359 杭州
121.482537 31.238034 上海
118.793117 32.068407 南京
二、基本命令
1. geoadd
為了進(jìn)行地理位置相關(guān)操作,我們首先需要將具體的地理位置記錄起來(lái),可以通過(guò)執(zhí)行g(shù)eoadd 命令來(lái)完成 命令格式如下
GEOADD 集合名稱 經(jīng)度 緯度 名稱 [longitude latitude name...] //添加集合 geoadd citys 120.70012 28.00135 wenzhou 120.207686 30.253359 hanghzou
查看已添加集合
2.geopos
此命令根據(jù)輸入的位置名稱獲取位置的信息坐標(biāo),語(yǔ)法如下
GEOPOS 集合名稱 位置 [name...] geopos address wuyue
查看坐標(biāo)信息
3.geodist
此命令用于計(jì)算兩個(gè)位置之間的距離,語(yǔ)法如下
geodist 集合名稱 位置1 位置2 [unit] //計(jì)算杭州到南京之間的距離 geodist citys hanghzredis:0>ou nanjing km
可選參數(shù):unit用于指定計(jì)算距離時(shí)的單位,他的值可以是以下單位的其中一個(gè)
m :表示米
km:表示千米
mi:表示英里
ft:表示英尺。
4.georadius
georadius使用用戶給定的經(jīng)緯度作為計(jì)算范圍時(shí)的中心點(diǎn),
georadius 集合名稱 精度 緯度 radius m|km|ft|mi| [WITHCOORD] [WITHDIST] [ASC|DESC] [COUNT count] //查詢據(jù)我100km之內(nèi)的城市 georadius citys 120.754274 27.983296 100 km
radius:距離
WITHCOORD:返回坐標(biāo)
由于版本原因可能為空
WITHDIST:同時(shí)返回距離
ASC|DESC:排序
count:取多少長(zhǎng)度
5. georadiusbymember
georadiusbymember使用存儲(chǔ)在位置集合里的某個(gè)地點(diǎn)作為中心點(diǎn)
georadiusbymember 地址集合 地點(diǎn)名稱 距離 單位 //查詢距離wuyue五公里之內(nèi)的地點(diǎn) georadiusbymember address wuyue 5 km
三、javaApi
實(shí)體類
package com.jiale.web.controller.pojo; import lombok.Data; import java.io.Serializable; /** * @Author: Hello World * @Date: 2021/9/16 16:12 * @Description: */ @Data public class AddressInfo implements Serializable { /**網(wǎng)點(diǎn)名稱*/ private String title; /**網(wǎng)點(diǎn)地址*/ private String address; /**網(wǎng)點(diǎn)聯(lián)系方式*/ private String phone; /**網(wǎng)點(diǎn)坐標(biāo)-精度*/ private double x; /**網(wǎng)點(diǎn)坐標(biāo)-緯度*/ private double y; }
package com.jiale; import com.jiale.common.config.JialeGlobal; import com.jiale.web.controller.pojo.AddressInfo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.geo.*; import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import java.math.BigDecimal; import java.util.List; /** * @Author: Hello World * @Date: 2021/9/15 17:47 * @Description: */ @SpringBootTest public class JialeTests { @Autowired RedisTemplate redisTemplate; @Test public void test1(){ System.out.println(JialeGlobal.getUploadPath()); } /** * 一.向redis中添加位置信息 * 1.網(wǎng)店位置信息-geo * 2.網(wǎng)點(diǎn)詳細(xì)信息 */ @Test public void geoAdd(){ //1.網(wǎng)點(diǎn)位置信息存儲(chǔ)120.653208,28.032606 Point point = new Point(120.653208, 28.032606); redisTemplate.boundGeoOps("outlets").add(point,"江心嶼"); //2.網(wǎng)點(diǎn)詳細(xì)信息 AddressInfo addressInfo = new AddressInfo(); addressInfo.setTitle("江心嶼"); addressInfo.setAddress("浙江省溫州市鹿城區(qū)江心嶼景區(qū)內(nèi)"); addressInfo.setPhone("(0577)88201281"); addressInfo.setX(120.653208); addressInfo.setY(28.032606); redisTemplate.boundHashOps("outletsInfo").put("江心嶼",addressInfo); } /** * 二.獲取位置坐標(biāo)信息 */ @Test public void geoPos(){ //傳入位置名稱查詢位置信息 List<Point> position = redisTemplate.boundGeoOps("outlets").position("南塘五組團(tuán)"); for (Point point : position) { System.out.println(point); } } /** * 三.計(jì)算兩個(gè)位置信息 */ @Test public void geoDist(){ /** Distance distance = redisTemplate.boundGeoOps("outlets").distance("江心嶼", "溫州樂(lè)園"); //距離 double value = distance.getValue(); //單位 String unit = distance.getUnit(); System.out.println("兩點(diǎn)相距:"+value+unit); */ //以km展示 /** Distance distance = redisTemplate.boundGeoOps("outlets").distance("江心嶼", "溫州樂(lè)園", Metrics.KILOMETERS); //距離 double value = distance.getValue(); //單位 String unit = distance.getUnit(); System.out.println("兩點(diǎn)相距:"+value+unit);*/ //保留兩位小數(shù) Distance distance = redisTemplate.boundGeoOps("outlets").distance("江心嶼", "溫州樂(lè)園", Metrics.KILOMETERS); //距離 double value = distance.getValue(); //單位 String unit = distance.getUnit(); System.out.println("兩點(diǎn)相距:"+new BigDecimal(value).setScale(2,BigDecimal.ROUND_HALF_UP) +unit); } /** * 四.按照給定的經(jīng)緯度查找指定范圍的位置 */ @Test public void geoRadius(){ //指定位置 Point point = new Point(120.754274, 27.983296); //構(gòu)建條件-10km Distance distance = new Distance(10, Metrics.KILOMETERS); Circle circle = new Circle(point,distance); //RedisGeoCommands RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs(); //包含位置信息 args.includeDistance(); //存放的是查詢到的網(wǎng)址位置信息 GeoResults<RedisGeoCommands.GeoLocation<String>> outlets = redisTemplate.boundGeoOps("outlets").radius(circle, args); for (GeoResult<RedisGeoCommands.GeoLocation<String>> outlet : outlets) { //獲取距離信息 Distance outletDistance = outlet.getDistance(); //距離 double value = outletDistance.getValue(); //單位 String unit = outletDistance.getUnit(); System.out.println("當(dāng)前坐標(biāo)距離:"+outlet.getContent().getName()+value+unit); } } /** * 五.按照指定元素(必須在集合中存在)查找指定范圍位置 */ @Test public void geoRadiusByMember(){ //構(gòu)建條件 Distance distance = new Distance(10, Metrics.KILOMETERS); //構(gòu)建條件-包含位置信息 RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs(); args.includeDistance(); //參數(shù) key 構(gòu)建的條件 其他條件 GeoResults<RedisGeoCommands.GeoLocation<String>> outlets = redisTemplate.boundGeoOps("outlets").radius("江心嶼", distance,args); for (GeoResult<RedisGeoCommands.GeoLocation<String>> outlet : outlets) { //獲取距離信息 Distance outletDistance = outlet.getDistance(); //距離 double value = outletDistance.getValue(); //單位 String unit = outletDistance.getUnit(); System.out.println("江心嶼距離:"+outlet.getContent().getName()+value+unit); } } }
到此這篇關(guān)于使用redis實(shí)現(xiàn)附近的人的文章就介紹到這了,更多相關(guān)redis實(shí)現(xiàn)附近的人內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Redis GEO實(shí)現(xiàn)搜索附近用戶的項(xiàng)目實(shí)踐
- Redis GEO地理信息定位功能
- Java中如何使用Redis GEO測(cè)算經(jīng)緯度距離
- 詳解Redis中地理位置功能Geospatial的應(yīng)用
- Redis特殊數(shù)據(jù)類型Geospatial地理空間
- Redis之GEO存儲(chǔ)地理位置信息的使用
- Redis 的 GeoHash詳解
- Redis中3種特殊的數(shù)據(jù)類型(BitMap、Geo和HyperLogLog)
- Redis 實(shí)現(xiàn)“附近的人”功能
- Redis用GEO實(shí)現(xiàn)附近的人功能
相關(guān)文章
從零搭建SpringBoot2.X整合Redis框架的詳細(xì)教程
這篇文章主要介紹了從零搭建SpringBoot2.X整合Redis框架的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12利用Redis實(shí)現(xiàn)訪問(wèn)次數(shù)限流的方法詳解
這篇文章主要給大家介紹了關(guān)于如何利用Redis實(shí)現(xiàn)訪問(wèn)次數(shù)限流的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Redis如何在項(xiàng)目中合理使用經(jīng)驗(yàn)分享
這篇文章主要給大家介紹了關(guān)于Redis如何在項(xiàng)目中合理使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04基于redis 7.2.3的makefile源碼解讀學(xué)習(xí)
這篇文章主要為大家介紹了基于redis 7.2.3的makefile源碼解讀學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12