| | |
| | | |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.geo.Circle; |
| | | import org.springframework.data.geo.GeoResult; |
| | | import org.springframework.data.geo.GeoResults; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.redis.connection.RedisGeoCommands; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | |
| | | public boolean unlock(){ |
| | | return unlock("redis"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检索坐标为中心直径范围内的数据,单位米 |
| | | * @param k |
| | | * @param lon |
| | | * @param lat |
| | | * @param radius |
| | | * @return |
| | | */ |
| | | public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> getNearGeo(String k, Double lon, Double lat, Double radius){ |
| | | Circle within = new Circle(lon, lat, radius); |
| | | GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius(k, within); |
| | | return geoResults.getContent(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检索坐标为中心直径范围内的数据,单位米,由近到远排序 |
| | | * @param k |
| | | * @param lon |
| | | * @param lat |
| | | * @param radius |
| | | * @return |
| | | */ |
| | | public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> getNearGeoSortAscending(String k, Double lon, Double lat, Double radius){ |
| | | Circle within = new Circle(lon, lat, radius); |
| | | RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs(); |
| | | geoRadiusCommandArgs.sortAscending(); |
| | | GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius(k, within, geoRadiusCommandArgs); |
| | | return geoResults.getContent(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 添加地理空间索引坐标 |
| | | * @param k |
| | | * @param lon |
| | | * @param lat |
| | | * @param object |
| | | */ |
| | | public void addGeo(String k, Double lon, Double lat, String object){ |
| | | Point point = new Point(lon, lat); |
| | | redisTemplate.opsForGeo().add(k, point, object); |
| | | } |
| | | } |