Java整合Redis實(shí)現(xiàn)坐標(biāo)附近查詢功能
一、GEO用法引入
- GEO,全稱Geolocation,代表地理坐標(biāo)??梢栽谄渲写鎯?chǔ)地理坐標(biāo)信息,幫助我們根據(jù)經(jīng)緯度來(lái)檢索數(shù)據(jù)。常見(jiàn)的命令有:
- GEOADD:添加一個(gè)或多個(gè)地理空間信息,包含:經(jīng)度(longitude)、緯度(latitude)、值(member)
- GEODIST:計(jì)算指定的兩個(gè)點(diǎn)之間的距離并返回
- GEOHASH:將指定member的坐標(biāo)轉(zhuǎn)為hash字符串形式并返回
- GEOPOS:返回指定member的坐標(biāo)
- GEORADIUS:指定圓心、半徑,找到該圓內(nèi)包含的所有member,并按照與圓心之間的距離排序后返回。(6.2以后已廢棄)
- GEOSEARCH:在指定范圍內(nèi)搜索member,并按照與指定點(diǎn)之間的距離排序后返回。范圍可以是圓形或矩形。(6.2以后新命令)
- GEOSEARCHSTORE:與GEOSEARCH功能一致,不過(guò)可以把結(jié)果存儲(chǔ)到一個(gè)指定的key。(6.2以后新命令)
我們可以在redis服務(wù)器使用命令 help xxx 查看指令的具體用法~
二、引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>三、實(shí)體類
1.返回實(shí)體
@Data
public class Geo<T> {
private T object;
private double distance;
}2.添加的商戶信息
@Data
@ApiModel("商戶信息DO")
public class CommercialDO implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.AUTO)
/**
* id
*/
private Integer id;
/**
* 商戶名稱
*/
private String name;
/**
* 商戶類型
*/
private String commercialType;
/**
* 門店名稱
*/
private String storeName;
/**
* 商戶地址
*/
private String address;
/**
* 項(xiàng)目code
*/
private String itemCode;
/**
* 經(jīng)度
*/
private String longitude;
/**
* 緯度
*/
private String latitude;
}四、添加位置信息
@ApiOperation("geo測(cè)試添加位置信息")
@GetMapping("/geo/add")
public String geoAdd(String key, double longitude, double latitude, CommercialDO commercialDO) throws Exception {
redisUtils.geoAdd(key, longitude, latitude, commercialDO);
return "添加成功";
} public void geoAdd(String key, double longitude, double latitude, Object object) {
redisTemplate.boundGeoOps(key).add(new Point(longitude, latitude),object);
}五、查詢位置信息
/**
* 根據(jù)經(jīng)緯度獲取指定距離范圍內(nèi)的地理位置信息
*
* @param key Redis中Geo操作的key
* @param longitude 經(jīng)度
* @param latitude 緯度
* @param distance 距離范圍(單位:米)
* @param limit 限制返回結(jié)果的數(shù)量
* @param searchName 地點(diǎn)名稱(可選)
* @return 返回地理位置信息的列表
*/
public List<Geo<T>> getNearbyByGeo(String key, double longitude, double latitude, int distance, int limit, String searchName) {
List<Geo<T>> geos = new ArrayList<>();
BoundGeoOperations boundGeoOperations = redisTemplate.boundGeoOps(key);
Point point = new Point(longitude, latitude);
Circle within = new Circle(point, distance);
RedisGeoCommands.GeoRadiusCommandArgs geoRadiusArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
geoRadiusArgs = geoRadiusArgs.includeDistance();
geoRadiusArgs.limit(limit);
geoRadiusArgs.sortAscending();
GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = boundGeoOperations.radius(within, geoRadiusArgs);
List<GeoResult<RedisGeoCommands.GeoLocation<Object>>> geoResultList = geoResults.getContent();
if (StringUtils.isNotBlank(searchName)) {
geoResultList = geoResults.getContent()
.stream()
.filter(result -> result.getContent().getName().toString().contains(searchName))
.collect(Collectors.toList());
}
Geo geo;
for (GeoResult<RedisGeoCommands.GeoLocation<Object>> geoResult : geoResultList) {
geo = new Geo();
geo.setObject(geoResult.getContent());
geo.setDistance(geoResult.getDistance().getValue());
geos.add(geo);
}
return geos;
}結(jié)果:會(huì)根據(jù)距離自動(dòng)排序,傳遞名稱則模糊查詢

到此這篇關(guān)于Java整合Redis實(shí)現(xiàn)坐標(biāo)附近查詢的文章就介紹到這了,更多相關(guān)Java整合Redis坐標(biāo)附近查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:快速計(jì)算二進(jìn)制數(shù)中1的個(gè)數(shù)(Fast Bit Counting)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:快速計(jì)算二進(jìn)制數(shù)中1的個(gè)數(shù)(Fast Bit Counting),本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06
springboot項(xiàng)目不輸出nohup.out日志的解決
這篇文章主要介紹了springboot項(xiàng)目不輸出nohup.out日志的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot中TransactionTemplate事務(wù)管理的實(shí)現(xiàn)
Spring Boot提供了多種方式來(lái)管理事務(wù),其中之一是使用TransactionTemplate,本文主要介紹了SpringBoot中TransactionTemplate事務(wù)管理的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
SpringBoot MainApplication類文件的位置詳解
這篇文章主要介紹了SpringBoot MainApplication類文件的位置詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤
這篇文章主要介紹了利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤,MDC?可以看成是一個(gè)與當(dāng)前線程綁定的哈希表,可以往其中添加鍵值對(duì),下文詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04
Java實(shí)現(xiàn)基礎(chǔ)銀行ATM系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)基礎(chǔ)銀行ATM系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Springboot的maven間接依賴的實(shí)現(xiàn)
這篇文章主要介紹了Springboot的maven間接依賴的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

