Java RedisGeoHash算法的使用以及详解

Java maven工程 操作RedisGeoHash

引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

java代码 看方法注释说明

	@Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加
     *
     * @param key       键名
     * @param precision 精度
     * @param dimension 维
     * @param name      位置名称
     * @return {@link Long}
     */
    @PostMapping("/redisGeoAdd")
    public Long redisGeoAdd(String key, double precision, double dimension, String name){
        return redisTemplate.opsForGeo().add(key,new Point(precision,dimension),name);
    }

    /**
     * 获得经纬度
     *
     * @param key      键名
     * @param nameList 位置名称集合
     * @return {@link List<Point>}
     */
    @PostMapping("/redisGeoGet/{key}")
    public List<Point> redisGeoGet( @PathVariable String key,@RequestBody List<String> nameList){
        return redisTemplate.opsForGeo().position(key,nameList.toArray());
    }

    /**
     * 获得两点之间距离
     *
     * @param key   键名
     * @param name1 位置1
     * @param name2 位置2
     * @return {@link Distance}
     */
    @GetMapping("/redisGeoDist")
    public Distance redisGeoDist(String key,String name1,String name2){
        return redisTemplate.opsForGeo().distance(key,name1,name2, RedisGeoCommands.DistanceUnit.KILOMETERS);
    }

    /**
     * 根据位置名称获得指定距离的位置信息
     *
     * @param key      键名
     * @param name     位置名称
     * @param distance 距离
     * @param count    数
     * @return {@link Object}
     */
    @GetMapping("/redisGeoRadius")
    public Object redisGeoRadius(String key,String name,Double distance,int count){
        RedisGeoCommands.GeoRadiusCommandArgs args;
        args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(count);
        return redisTemplate.opsForGeo().radius(key,name,new Distance(distance, Metrics.KILOMETERS),args);
    }

    /**
     * 获得位置geohash表示
     *
     * @param key      键名
     * @param nameList 位置名称
     * @return {@link List<String>}
     */
    @PostMapping("/redisGeoHash/{key}")
    public List<String> redisGeoHash( @PathVariable String key,@RequestBody List<String> nameList){
        return redisTemplate.opsForGeo().hash(key,nameList.toArray());
    }

java调用百度地图API根据位置名称获得经纬度

这里使用spring的restful风格调用

	@Resource
    private RestTemplate restTemplate;

    String ak="百度ak";

    /**
     * 根据位置名称获得经纬度  
     *
     * @param address 地址  越详细精度越准确
     * @return {@link Object}
     */
    @GetMapping("/getXYByName")
    public Object getXYByName(String address){
        String forObjectToString = restTemplate.getForObject(
                "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + ak + "&callback=showLocation ",
                String.class
        );
        JSONObject jsonObject = JSONUtil.parseObj(forObjectToString);
        if (jsonObject.getStr("status").equals("0")) {
            //成功
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            Double lng = location.getDouble("lng");
            Double lat = location.getDouble("lat");

        }
        return jsonObject;
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小浪吴啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值