Java maven工程 操作RedisGeoHash
引入依赖
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-data-redis</artifactId> |
| </dependency> |
java代码 看方法注释说明
| @Autowired |
| private RedisTemplate redisTemplate; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @PostMapping("/redisGeoAdd") |
| public Long redisGeoAdd(String key, double precision, double dimension, String name){ |
| return redisTemplate.opsForGeo().add(key,new Point(precision,dimension),name); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| @PostMapping("/redisGeoGet/{key}") |
| public List<Point> redisGeoGet( @PathVariable String key,@RequestBody List<String> nameList){ |
| return redisTemplate.opsForGeo().position(key,nameList.toArray()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @GetMapping("/redisGeoDist") |
| public Distance redisGeoDist(String key,String name1,String name2){ |
| return redisTemplate.opsForGeo().distance(key,name1,name2, RedisGeoCommands.DistanceUnit.KILOMETERS); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @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); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| @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"; |
| |
| |
| |
| |
| |
| |
| |
| @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; |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 数据并发安全校验处理工具类