redisGeo的相关命令
1. GEOADD key longitude latitude member [longitude latitude member …]
2. GEODIST key member1 member2 [unit]
3 .GEOHASH key member [member …]
4. GEOPOS key member [member …]
5. GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD][WITHDIST] [WITHHASH][COUNT count] [ASC|DESC][STORE key] [STOREDIST key]
6.GEORADIUSBYMEMBER key member radius [m|km|ft|mi] [WITHCOORD] [WITHDIST] [ASC|DESC] [WITHHASH] [COUNT count]
redisGeo命令简单介绍
- geoadd 为成员增加某个地理位置的坐标
- geodist 获取两个成员之间的地理位置的距离,可以设置距离单位 m:米 ,km:千米,ft:英尺 ,mi:英里
- geohash 获取某个地理位置的hash值
- geopos 获取某个成员的地理位置的坐标
- georadius 根据给定地理位置坐标获取指定范围内的地理位置集合
- WITHCOORD:传入WITHCOORD参数,则返回结果会带上匹配位置的经纬度。
- WITHDIST:传入WITHDIST参数,则返回结果会带上匹配位置与给定地理位置的距离。
- ASC|DESC:默认结果是未排序的,传入ASC为从近到远排序,传入DESC为从远到近排序。
- WITHHASH:传入WITHHASH参数,则返回结果会带上匹配位置的hash值。
- COUNT count:传入COUNT参数,可以返回指定数量的结果。
- georadiusbymember 根据给定成员获取指定范围内的地理位置集合,相对于georadius命令,使用起来比较方便.参数基本一样
jedis使用Geo功能
1.为成员增加某个地理位置的坐标
public void addLatAndLng(Double lat, Double lng, String id) {
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
boundGeoOps.geoAdd(new Point(lng, lat), Id);
}
2.获取两个成员之间的地理位置的距离,可以设置距离单位
public String getDistance(String fromMember, String toMember) {
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
//默认的单位为米
// boundGeoOps.geoDist(fromMember, toMember, Metrics.MILES);可以设置距离单位
Distance geoDist = boundGeoOps.geoDist(fromMember, toMember);
double value = geoDist.getValue();
return value;
}
3.获取某个成员的地理位置的坐标
public List<Point> getLocations(String member) {
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
List<Point> geoPos = boundGeoOps.geoPos(Member);
return geoPos;
}
4.根据给定地理位置坐标获取指定范围内的地理位置集合
public GeoResults<GeoLocation<Object>> getRangeDistance(Double lng, Double lat, Double range, int count) {
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
Circle circle = new Circle(new Point(lng, lat), new Distance(range, Metrics.KILOMETERS));
//可以指定分页数量,以及排序规则
GeoResults<GeoLocation<Object>> geoRadius = boundGeoOps.geoRadius(circle,
GeoRadiusCommandArgs.newGeoRadiusArgs().sortDescending().limit(count));
return geoRadius;
}
5.获取某个地理位置的hash值
public List<String> getPositionHash(String Member){
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
List<String> hashLists = boundGeoOps.geoHash(Member);
return hashLists ;
}
6.根据给定成员获取指定范围内的地理位置集合
public GeoResults<GeoLocation<Object>> getRangeDistanceByMember(String member, Double range, int count) {
BoundGeoOperations<Serializable, Object> boundGeoOps = redisTemplate.boundGeoOps(RedisKeyConstant.USER_GEO_KEY);
GeoResults<GeoLocation<Object>> geoResults = boundGeoOps.geoRadiusByMember(member, new Distance(range, Metrics.KILOMETERS), GeoRadiusCommandArgs.newGeoRadiusArgs().sortDescending().limit(count));
return geoResults;
}