代码块语法遵循标准markdown代码,例如:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.geo.GeoRadiusParam;
/**
*
* @author
*
*/
public class JedsiR {
static String host = "localhost";
static int port = 6379;
static Jedis jedis = new Jedis(host, port);
/**
* 添加到redius
* @param key redius的键
* @param longitude 经度
* @param latitude 纬度
* @param member 用户标识
*
@author: jun-qi.hu
*
createDate:2017年2月28日上午10:48:51
*/
public void geoadd(String key, double longitude, double latitude, String member) {
//单数据插入
jedis.geoadd(key, longitude, latitude, member);
Map coordinateMap = new HashMap();
/**
* @param key a= redius的键
* @param value 3=经度,4=纬度
*/
coordinateMap.put("a", new GeoCoordinate(3, 4));
coordinateMap.put("b", new GeoCoordinate(2, 3));
coordinateMap.put("c", new GeoCoordinate(3.314, 2.3241));
// 批量插入 键/值对
jedis.geoadd("foo", coordinateMap);
}
// 2.查询两地距离 距离单位
public void geodist(String key, String member1, String member2, GeoUnit unit) {
//不输入则使用默认单位
Double dist = jedis.geodist(key, member1, member2);
dist = jedis.geodist(key, member1, member2, unit);
}
// 查询指定位置坐标都HASH编码
public List geohash(String key,String... array) {
List hashes = jedis.geohash(key, array);
return hashes;
}
// GEOPOS 查询多个成员位置的坐标
public void geopos(String key, String... members) {
List coordinates = jedis.geopos("foo", members);
}
// GEORADIUS 查询某经纬度定位的附近其它成员
public void georadius(String key, double longitude, double latitude, double radius, GeoUnit unit) {
List members = jedis.georadius(key, longitude, latitude, radius, unit);
// sort 查询附近并根据距离排序
members = jedis.georadius("Sicily", 15, 37, 200, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().sortAscending());
// sort
members = jedis.georadius("Sicily", 15, 37, 200, GeoUnit.KM,
GeoRadiusParam.geoRadiusParam().sortAscending().count(1));
// withdist, withcoord 这是个什么鬼! 我也没去查。。。 有谁知道类 麻烦告诉我啊
members = jedis.georadius("Sicily", 15, 37, 200, GeoUnit.KM,
GeoRadiusParam.geoRadiusParam().sortAscending().count(1).withCoord().withDist());
GeoRadiusResponse response = members.get(0);
}
// GEORADIUS 查询该成员定位的附近其它成员
public void georadiusByMember(String key, String member, double radius, GeoUnit unit) {
List members = jedis.georadiusByMember(key, member, radius, unit);
// 将结果升序排列
members = jedis.georadiusByMember("Sicily", "Agrigento", 100, GeoUnit.KM,
GeoRadiusParam.geoRadiusParam().sortAscending());
members = jedis.georadiusByMember("Sicily", "Agrigento", 100, GeoUnit.KM,
GeoRadiusParam.geoRadiusParam().sortAscending().count(1).withCoord().withDist());
}
}