redisTempalte geo 常用方法
时间: 2023-11-17 16:03:04 浏览: 101
Template中的geo方法主要用于处理地理位置类型的数据,以下是一些常用的方法:
```java
// 添加地理位置信息
GeoOperations<String, String> geoOps = redisTemplate.opsForGeo();
geoOps.add("city", new Point(116.405285, 39.904989), "Beijing");
geoOps.add("city", new Point(121.472644, 31.231706), "Shanghai");
// 获取两个地理位置之间的距离
Distance distance = geoOps.distance("city", "Beijing", "Shanghai", Metrics.KILOMETERS);
System.out.println(distance.getValue() + " " + distance.getUnit());
// 获取指定地理位置的经纬度
List<Point> points = geoOps.position("city", "Beijing", "Shanghai");
for (Point point : points) {
System.out.println(point.getX() + " " + point.getY());
}
// 获取指定地理位置范围内的所有地理位置信息
Circle circle = new Circle(new Point(116.405285, 39.904989), new Distance(500, Metrics.KILOMETERS));
GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = geoOps.radius("city", circle);
for (GeoResult<RedisGeoCommands.GeoLocation<String>> geoResult : geoResults) {
System.out.println(geoResult.getContent().getName() + " " + geoResult.getDistance().getValue() + " " + geoResult.getDistance().getUnit());
}
```
阅读全文