兒童定位手表,有個交友功能,查找附近的人,用redis的geo來實現比較簡單,其實是一個ZSET(有序集合)
redis 版本要大於3.2
查看redis 版本 /usr/bin/redis-server --version
注意引入的jar版本:可能運行時候會報錯,這時要檢查jar包的版本,可能版本沖突導致報錯
public class Coordinate { //經度 private double longitude; //緯度 private double latitude; //用戶id private String key; public double getLatitude() { return latitude; } public void setLatitude(double latitude) { this.latitude = latitude; } public double getLongitude() { return longitude; } public void setLongitude(double longitude) { this.longitude = longitude; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } }
public class RedisUtil { private static JedisPool jedisPool = null; // Redis服務器IP private static String ADDR = "xx.xxx.xx.xx"; // Redis的端口號 private static int PORT = 6379; // 訪問密碼 private static String AUTH = "xxxxxx"; /** * 初始化Redis連接池 */ static { try { JedisPoolConfig config = new JedisPoolConfig(); // 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true config.setBlockWhenExhausted(true); // 設置的逐出策略類名, 默認DefaultEvictionPolicy(當連接超過最大空閑時間,或連接數超過最大空閑連接數) config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy"); // 是否啟用pool的jmx管理功能, 默認true config.setJmxEnabled(true); // 最大空閑連接數, 默認8個 控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。 config.setMaxIdle(8); // 最大連接數, 默認8個 config.setMaxTotal(200); // 表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException; config.setMaxWaitMillis(1000 * 100); // 在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的; config.setTestOnBorrow(true); jedisPool = new JedisPool(config, ADDR, PORT, 3000, AUTH); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取Jedis實例 * * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * 釋放jedis資源 * * @param jedis */ public static void close(final Jedis jedis) { if (jedis != null) { jedis.close(); } } public static void main(String[] args) { Jedis jedis = RedisUtil.getJedis(); // //添加經緯度 // Coordinate coordinate=new Coordinate(); // coordinate.setLatitude(31.244803); //維度 // coordinate.setLongitude(121.483671); //經度 // coordinate.setKey("zhangsan"); //可以作為用戶表的id // // // //添加經緯度 // Coordinate coordinate1=new Coordinate(); // coordinate1.setLatitude(31.245321); //維度 // coordinate1.setLongitude(121.485015); //經度 // coordinate1.setKey("lisi"); //可以作為用戶表的id // // //添加經緯度 // Coordinate coordinate2=new Coordinate(); // coordinate2.setLatitude(31.245456); //維度 // coordinate2.setLongitude(121.485285); //經度 // coordinate2.setKey("wangwu"); //可以作為用戶表的id // // addReo(coordinate); // addReo(coordinate1); // addReo(coordinate2); // Coordinate query = new Coordinate(); // query.setLongitude(121.485285); // query.setLatitude(31.245456); // // List<GeoRadiusResponse> result = geoQuery(query); // for(GeoRadiusResponse res : result) { // System.out.println(res.getMemberByString()); // } RedisUtil.close(jedis); } /** * 添加坐標 * key 經度 維度 距離 * return m 表示單位為米*/ public static Long addReo(Coordinate coordinate) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //第一個參數可以理解為表名 return jedis.geoadd("test",coordinate.getLongitude(),coordinate.getLatitude(),coordinate.getKey()); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (null != jedis) jedis.close(); } return null; } /** * 查詢附近人 * key 經度 維度 距離 * return GeoRadiusResponse*/ public static List<GeoRadiusResponse> geoQuery(Coordinate coordinate) { Jedis jedis = null; try { jedis = jedisPool.getResource(); //200F GeoUnit.KM表示km return jedis.georadius("test",coordinate.getLongitude(),coordinate.getLatitude(),100F,GeoUnit.M, GeoRadiusParam.geoRadiusParam().withDist()); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (null != jedis) jedis.close(); } return null; } }
引用相關jar
轉載自:https://blog.csdn.net/liaodehong/article/details/59104451