private final StringRedisTemplate redisTemplate;
/**
* 向指定key中添加经纬度信息
*
* @param key 存储的键值
* @param longitude 经度
* @param latitude 纬度
* @param member 成员名称(数据的标识 比如userID)
* @return 添加成功的条数
*/
public long geoAdd(String key, Double longitude, Double latitude, String member) {
return redisTemplate.opsForGeo().add(key, new Point(longitude, latitude), member);
}
/**
* 向指定key中添加经纬度信息
*
* @param key
* @param locations
* @return
*/
public long geoAddList(String key, List> locations) {
return redisTemplate.opsForGeo().add(key, locations);
}
/**
* 获取成员之间的距离
*
* @param key 存储的键值
* @param member1 成员名称
* @param member2 成员名称
* @return
*/
public Distance geoDist(String key, String member1, String member2) {
return redisTemplate.opsForGeo().distance(key, member1, member2);
}
/**
* 获取指定key值的所有成员列表
*
* @param key
* @return
*/
public List geoHash(String key) {
return redisTemplate.opsForGeo().hash(key);
}
/**
* 获取指定key值的所有成员列表
*
* @param key
* @return
*/
public List geoHashList(String key, String[] serviceStations) {
GeoOperations ops = redisTemplate.opsForGeo();
return ops.hash(key, serviceStations);
}
/**
* 获取指定成员的位置信息
*
* @param key 存储的键值
* @param member 成员名称
* @return
*/
public Point geoPos(String key, String member) {
return (redisTemplate.opsForGeo().position(key, member)).get(0);
}
/**
* 获取指定成员 范围内的位置信息
*
* @param key 存储的键值
* @param member 成员名称
* @param distance 距离
* @return
*/
public GeoResults> geoRadius(String key, String member, double distance) {
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates()
.sortAscending();
return redisTemplate.opsForGeo().radius(key, member, new Distance(distance, Metrics.KILOMETERS), args);
}
/**
* 根据给定地理位置坐标获取指定范围内的地理位置集合
*
* @param key 存储的键值
* @param lng 经度
* @param lat 纬度
* @param distance 距离(公里)
* @return {@link RedisGeoCommands.GeoLocation}
*/
public GeoResults> getPointRadius(String key, Double lng, Double lat, int distance) {
Circle within = new Circle(new Point(lng, lat), new Distance(distance, Metrics.KILOMETERS));
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.includeDistance() //包含距离
.includeCoordinates() //包含经纬度
.sortAscending()//正序排序
.limit(50); //条数
return redisTemplate.opsForGeo().radius(key, within, args);
}
/**
* 批量删除
*
* @param key 存储的键值
* @param name 成员名称
* @return
*/
public Long deleteBatchRedisGeo(String key, String[] name) {
return redisTemplate.boundZSetOps(key).remove(name);
}
/**
* 单个删除
*
* @param key 存储的键值
* @param name 成员名称
* @return
*/
public Long deleteRedisGeo(String key, String name) {
return redisTemplate.boundZSetOps(key).remove(name);
}
/**
GEO key 常量
*/
public class RedisGeo {
/**
/**
@Data
public class QueryNearDTO {
/**
* 距离 (公里)
*/
@ApiModelProperty(value = "距离 (公里)", required = true)
@NotNull
private Integer distance;
/**
* 租户id
*/
@ApiModelProperty(value = "租户id", hidden = true)
private String tenantId;
/**
* 经度
*/
@ApiModelProperty(value = "经度", required = true)
@NotNull
private Double lng;
/**
* 纬度
*/
@ApiModelProperty(value = "纬度", required = true)
@NotNull
private Double lat;
/**
* 获取条数
*/
@ApiModelProperty(value = "获取条数")
private Integer limit;
}
public class TestController {
private final RedisGeoUtil redisGeoUtil;
@PostMapping("a_geo")
@ApiOperation(value = "批量添加附近坐标", notes = "添加附近坐标")
public R addGeo() {
List> list = new ArrayList<>();
double[] points = {113.674755, 34.743285,
113.665556, 34.753013,
113.731959, 34.74376,
113.662538, 34.720501,
113.668287, 34.768197,
113.668287, 34.768197,
113.671881, 34.749158,
113.671881, 34.749158, 113.668287, 34.768197,
113.668287, 34.768197,
113.671881, 34.749158,
113.671881, 34.749158};
for (int i = 0; i < 20; i += 2) {
RedisGeoCommands.GeoLocation location = new RedisGeoCommands.GeoLocation("name_" + i / 2, new Point(points[i], points[i + 1]));
list.add(location);
}
long count = redisGeoUtil.geoAddList(RedisGeo.redisKeyCommunityCode + "123456", list);
return R.data(count);
}
@GetMapping("q_geo_add")
@ApiOperation(value = "单个新增坐标")
public R q_geo_add() {
return R.data(redisGeoUtil.geoAdd( RedisGeo.redisKeyCommunityCode + "000000", new Double(113.543099), new Double(34.743285), "666666"));
}
@GetMapping("q_geo_if")
@ApiOperation(value = "查询某点附近距离信息")
public R getGeo1(String member, double distance) {
GeoResults> geoResults = redisGeoUtil.geoRadius(RedisGeo.redisKeyCommunityCode + "000000", member, distance);
return R.data(geoResults);
}
@GetMapping("q_geo_local")
@ApiOperation(value = "查询坐标范围内数据", notes = "查询范围内数据")
public R>> q_geo_local(QueryNearDTO queryNearDTO) {
return R.data(redisGeoUtil.getPointRadius(RedisGeo.redisKeyCommunityCode + "000000", queryNearDTO.getLng(), queryNearDTO.getLat(), queryNearDTO.getDistance(),1));
}
@GetMapping("q_geo_remove")
@ApiOperation(value = "删除坐标信息", notes = "删除坐标信息")
public R q_geo_remove(String member) {
String[] aa=Arrays.asList("中石化", "666666", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
return R.data(redisGeoUtil.deleteBatchRedisGeo(RedisGeo.redisKeyCommunityCode + "000000", aa));
}
/**
* 测试 获取某个地理位置的 geohash 值
* */
@GetMapping("q_hash_list")
@ApiOperation(value = "获取地理位置的 geohash 值")
public R> testGetServiceStationGeoHash() {
String[] aa=Arrays.asList("中石化", "中石油", "REDIS_GEO_CABINET_COMMUNITY:504756").toArray(new String[3]);
List hashList= redisGeoUtil.geoHashList(RedisGeo.redisKeyCommunityCode + "000000",aa);
return R.data(hashList);
}