redis-RedisTemplate.opsForGeo 的geo地理位置相关的方法演示
·
主要方法:
add : 添加一个地理位置
distance: 计算两个元素之间的距离
hash: 获取元素经纬度坐标经过geohash算法生成的base32编码值
position: 获取集合中任意元素的经纬度坐标,可以一次获取多个
radius:查询某个坐标或某个成员,附近范围的成员
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class Controller_redis_geo_1 {
@Autowired
private RedisTemplate redisTemplate;
private final String KEY_GEO = "home";
@RequestMapping("/testGeo")
public void testGeo() {
// geoadd : 添加一个地理位置
// geoadd : (纬度、经度、名称)三元组
redisTemplate.opsForGeo().add(KEY_GEO, new Point(116.48105, 39.996794), "张三");
redisTemplate.opsForGeo().add(KEY_GEO, new Point(116.514203, 39.905409), "李四");
redisTemplate.opsForGeo().add(KEY_GEO, new Point(116.489033, 40.007669), "王五");
System.out.println();
// geodist : 计算两个元素之间的距离
// 计算张三李四之间的距离 其中,距离单位可以是m、km、ml、ft,分别代表米、千米、英里和尺
Distance distance = redisTemplate.opsForGeo().distance(KEY_GEO, "张三", "李四");
Distance distance2 = redisTemplate.opsForGeo().distance(KEY_GEO, "张三", "李四", RedisGeoCommands.DistanceUnit.KILOMETERS);
System.out.println(distance);
System.out.println(distance2.getValue());
System.out.println();
// geohash : 获取元素经纬度坐标经过geohash算法生成的base32编码值
// 注意:GeoHash对二维经纬度坐标进行一维映射是有损的,通过映射再还原回的经纬度坐标和原始输入的经纬度坐标存在一定的误差。
List<String> hash = redisTemplate.opsForGeo().hash(KEY_GEO, "张三");
List<String> hashs = redisTemplate.opsForGeo().hash(KEY_GEO, "张三", "李四");
System.out.println(hash);
System.out.println(hashs);
System.out.println();
// geopos : 获取集合中任意元素的经纬度坐标,可以一次获取多个
List<Point> position = redisTemplate.opsForGeo().position(KEY_GEO, "张三");
System.out.println(position);
List<Point> positions = redisTemplate.opsForGeo().position(KEY_GEO, "张三", "李四");
System.out.println(positions);
System.out.println();
// GEORADIUS以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素。
// 通过经度,纬度查找附近的人
Circle circle = new Circle(116.48105, 39.996794, Metrics.KILOMETERS.getMultiplier());
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.includeDistance() //包含距离
.includeCoordinates() //包含坐标
.sortAscending() //升序
.limit(50);
GeoResults<RedisGeoCommands.GeoLocation<Object>> byxy = redisTemplate.opsForGeo().radius(KEY_GEO, circle, args);
System.out.println("通过经纬度附近的人:");
byxy.forEach(r -> System.out.println(r));
System.out.println();
// 通过地方查找附近5km的2个人
RedisGeoCommands.GeoRadiusCommandArgs args2 = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
.includeDistance()
.includeCoordinates()
.sortAscending()
.limit(2);
GeoResults<RedisGeoCommands.GeoLocation<Object>> radius = redisTemplate.opsForGeo().radius(KEY_GEO, "张三", new Distance(5, Metrics.KILOMETERS), args2);
System.out.println("通过名字附近的人:");
radius.forEach(r -> System.out.println(r));
}
}
更多推荐


所有评论(0)