org.gavaghan geodesy 1.1.3
// 计算坐标相差距离
public static void main(String[] args) {
//纬度,经度
GlobalCoordinates source = new GlobalCoordinates(29.490295, 106.486654);
GlobalCoordinates target = new GlobalCoordinates(29.615467, 106.581515);
// Ellipsoid.Sphere Ellipsoid.WGS84 坐标系
//创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere,source, target);
double ellipsoidalDistance = geoCurve.getEllipsoidalDistance();
System.out.println("坐标系计算结果 : " +ellipsoidalDistance + "米");
}
redis使用Geo
添加坐标到Redis
HashMappoints = new HashMap<>(); //(lng,lat) 经纬度 points.put("tianjin", new Point(117.12, 39.08)); points.put("shijiazhuang", new Point(114.29, 38.02)); redisTemplate.boundGeoOps("zhk").add(points);
添加单个坐标到redis中
//城市 经,纬
RedisGeoCommands.GeoLocation tianjin = new RedisGeoCommands.GeoLocation<>(
"tianjin", new Point(117.12, 39.08));
redisTemplate.opsForGeo().add("zhk",tianjin);
//或者
Point point = new Point(117.12, 39.08)
redisTemplate.opsForGeo().add("zhk", point,"tianjin");
查询附近100km的坐标
//查找附近坐标 以 Point point = new Point(115.03, 38.44);为标准
// Circle对象是封装覆盖的面积,需要的要素为中心点坐标Point对象、半径(radius)、计量单位(metric)
Point point = new Point(115.03, 38.44);
Metric metric = RedisGeoCommands.DistanceUnit.KILOMETERS;
Distance distance = new Distance(100, metric);
Circle circle = new Circle(point, distance);
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands
.GeoRadiusCommandArgs
.newGeoRadiusArgs()
.includeDistance()
.includeCoordinates()
.sortAscending()
.limit(10);
GeoResults> radius = redisTemplate.opsForGeo()
.radius("zhk", circle, args);
if (radius != null) {
radius.forEach(geoLocationGeoResult -> {
RedisGeoCommands.GeoLocation content = geoLocationGeoResult.getContent();
//member 名称 如 tianjin
String name = content.getName();
System.out.println(name);
// 对应的经纬度坐标
Point pos = content.getPoint();
System.out.println(pos);
// 距离中心点的距离
Distance dis = geoLocationGeoResult.getDistance();
System.out.println(dis);
});
}
获取某个城市之间10000km的城市名字 包括当前城市名字
//获取某个城市附近100km的范围
Distance distance = new Distance(10000, Metrics.KILOMETERS);
//可在distance后,加如上述args
GeoResults> radius = redisTemplate.opsForGeo().radius("zhk", "tianjin", distance);
radius.forEach(
geoLocationGeoResult -> {
RedisGeoCommands.GeoLocation content = geoLocationGeoResult.getContent();
String name = content.getName();
System.out.println(name);
}
);
删除元素
redisTemplate.boundZSetOps("zhk").remove("tianjin");
获取定位城市的坐标
Listposition = redisTemplate.opsForGeo().position("zhk", "tianjin"); position.forEach(System.out::println);
获取两个城市的距离
//获取两个城市之间的距离
//米
double value = redisTemplate.opsForGeo().distance("zhk", "tianjin", "shijiazhuang").getValue();
//km
double value1 = redisTemplate.opsForGeo().distance("zhk", "tianjin", "shijiazhuang", Metrics.KILOMETERS).getValue();
获取geohash值
//我们把每个人的经纬度都转换成geohash,
// 根据geohash选择周围的事物距离,比如截取geohash前5位,
// 进行对比,前五位全部相同,说明这些geohash距离是2.4公里以内的(微信的附近人最远就是2.5公里,估计也是这个东西搞出来的)
//4位20KM
//5位2.4KM
//6位0.61KM
//7位0.076KM
//8位0.019KM
redisTemplate.opsForGeo().hash("zhk", Arrays.asList("tianjin", "shijiazhuang", "xxx").toArray(new String[3])).forEach(
System.out::println
);
redis使用set
//添加
redisTemplate.opsForSet().add("keyname", "1000")
//是否包含 返Boolean
redisTemplate.opsForSet().isMember("keyname", "1000")
暑期编程PK赛
得CSDN机械键盘等精美礼品!


