RedisTemplate:geohash地址位置工具类

自己写的geo地理位置工具类

package com.example.redistest.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @Author lie
 * @Description
 */
@Component
public class GeoHashUtil {

    @Autowired(required = false)
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 添加节点及位置信息
     * @param geoKey 位置集合
     * @param pointName 位置点标识
     * @param longitude 经度
     * @param latitude 纬度
     */
    public Long geoAdd(String geoKey, double longitude, double latitude, String pointName){
        Point point = new Point(longitude, latitude);
        return redisTemplate.opsForGeo().add(geoKey, point, pointName);
    }

    /**
     * 以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素,并给出所有位置元素与中心的平均距离
     * @param geoKey    key
     * @param longitude 经度
     * @param latitude 维度
     * @param radius    距离
     * @param metricUnit 距离单位,例如 Metrics.KILOMETERS
     * @param limit 人数
     */
    public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> findRadius(String geoKey
            , double longitude, double latitude, double radius, Metrics metricUnit, int limit){
        // 设置检索范围
        Point point = new Point(longitude, latitude);
        Circle circle = new Circle(point, new Distance(radius, metricUnit));
        // 定义返回结果参数,如果不指定默认只返回content即保存的member信息
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
                .newGeoRadiusArgs().includeDistance().includeCoordinates()
                .sortAscending()
                .limit(limit);
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo().radius(geoKey, circle, args);
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();
        return list;
    }

    /**
     * 计算指定key下两个成员点之间的距离
     * @param geoKey  key
     * @param member1 位置1
     * @param member2 位置2
     * @param unit 单位
     */
    public Distance calDistance(String geoKey, String member1, String member2
            , RedisGeoCommands.DistanceUnit unit){
        Distance distance = redisTemplate.opsForGeo()
                .distance(geoKey, member1, member2, unit);
        return distance;
    }

    /**
     * 根据成员点名称查询位置信息
     * @param geoKey geo key
     * @param members 名称数组
     */
    public List<Point> geoPosition(String geoKey, String[] members){
        List<Point> points = redisTemplate.opsForGeo().position(geoKey, members);
        return points;
    }

    /**
     * 以给定的城市为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素,并给出所有位置元素与中心的平均距离。
     * @param key redis的key
     * @param name 名称
     * @param distance 距离
     * @param count 人数
     */
    public GeoResults<RedisGeoCommands.GeoLocation<String>> geoNearByPlace(String key, String name, Integer distance, Integer count) {
        //params: 距离量, 距离单位
        Distance distances = new Distance(distance, Metrics.KILOMETERS);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(count);
        //params: key, 地方名称, Circle, GeoRadiusCommandArgs
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo().radius(key, name, distances, args);
        return results;
    }


    /**
     * 返回一个或多个位置元素的 Geohash 表示
     * @param key redis的key
     * @param members  名称的数组
     */
    public List<String> geoHash(String key, String[] members) {
        //params: key, 地方名称...
        List<String> results = redisTemplate.opsForGeo().hash(key, members);
        return results;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RedisTemplateSpring框架提供的一个Redis操作工具类,封装了Redis连接池的操作。在使用Redis的时候,可以通过RedisTemplateRedis进行操作,包括String、Hash、List、Set、ZSet、HyperLogLog等数据结构的操作。 在使用RedisTemplate进行geo操作时,需要先设置序列化方式为GenericJackson2JsonRedisSerializer,然后通过GeoOperations接口进行操作。GeoOperations包括添加地理位置信息、获取两个位置之间的距离、获取指定位置的附近的其他位置信息等操作。 以下是RedisTemplate工具类的示例代码: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } } @Service public class RedisGeoService { @Autowired private RedisTemplate<String, Object> redisTemplate; public Object addGeo(String key, GeoLocation<Object> location) { GeoOperations<String, Object> geoOperations = redisTemplate.opsForGeo(); return geoOperations.add(key, location); } public Object getDistance(String key, Object member1, Object member2) { GeoOperations<String, Object> geoOperations = redisTemplate.opsForGeo(); return geoOperations.distance(key, member1, member2); } public Object getRadius(String key, Object member, double radius, Metric metric) { GeoOperations<String, Object> geoOperations = redisTemplate.opsForGeo(); return geoOperations.radius(key, member, new Distance(radius, metric)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值