【探花交友】day07—搜附近(三)

简介: 【探花交友】day07—搜附近(三)

3.1、dubbo服务

3.1.1、定义pojo

在my-tanhua-dubbo-interface中创建:

1. @Data
2. @NoArgsConstructor
3. @AllArgsConstructor
4. @Document(collection = "user_location")
5. @CompoundIndex(name = "location_index", def = "{'location': '2dsphere'}")
6. public class UserLocation implements java.io.Serializable{
7. 
8. private static final long serialVersionUID = 4508868382007529970L;
9. 
10. @Id
11. private ObjectId id;
12. @Indexed
13. private Long userId; //用户id
14. private GeoJsonPoint location; //x:经度 y:纬度
15. private String address; //位置描述
16. private Long created; //创建时间
17. private Long updated; //更新时间
18. private Long lastUpdated; //上次更新时间
19. }

3.1.2、定义dubbo接口

在my-tanhua-dubbo-interface工程中。

1. package com.tanhua.dubbo.server.api;
2. 
3. public interface UserLocationApi {
4. 
5. //更新地理位置
6.     Boolean updateLocation(Long userId, Double longitude, Double latitude, String address);
7. }

3.1.3、编写实现

1. @DubboService
2. public class UserLocationApiImpl implements UserLocationApi{
3. 
4. @Autowired
5. private MongoTemplate mongoTemplate;
6. 
7. //更新地理位置
8. public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
9. try {
10. //1、根据用户id查询位置信息
11. Query query = Query.query(Criteria.where("userId").is(userId));
12. UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
13. if(location == null) {
14. //2、如果不存在用户位置信息,保存
15.                 location = new UserLocation();
16.                 location.setUserId(userId);
17.                 location.setAddress(address);
18.                 location.setCreated(System.currentTimeMillis());
19.                 location.setUpdated(System.currentTimeMillis());
20.                 location.setLastUpdated(System.currentTimeMillis());
21.                 location.setLocation(new GeoJsonPoint(longitude,latitude));
22.                 mongoTemplate.save(location);
23.             }else {
24. //3、如果存在,更新
25. Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
26.                         .set("updated", System.currentTimeMillis())
27.                         .set("lastUpdated", location.getUpdated());
28.                 mongoTemplate.updateFirst(query,update,UserLocation.class);
29.             }
30. return true;
31.         } catch (Exception e) {
32.             e.printStackTrace();
33. return false;
34.         }
35.     }
36. }

3.1.4、单元测试

地理位置坐标拾取:拾取坐标系统

1. @RunWith(SpringRunner.class)
2. @SpringBootTest(classes = AppServerApplication.class)
3. public class TestUserLocationApi {
4. 
5. @DubboReference
6. private UserLocationApi userLocationApi;
7. 
8. @Test
9. public void testUpdateUserLocation() {
10. this.userLocationApi.updateLocation(1L, 116.353885,40.065911, "育新地铁站");
11. this.userLocationApi.updateLocation(2L, 116.352115,40.067441, "北京石油管理干部学院");
12. this.userLocationApi.updateLocation(3L, 116.336438,40.072505, "回龙观医院");
13. this.userLocationApi.updateLocation(4L, 116.396797,40.025231, "奥林匹克森林公园");
14. this.userLocationApi.updateLocation(5L, 116.323849,40.053723, "小米科技园");
15. this.userLocationApi.updateLocation(6L, 116.403963,39.915119, "天安门");
16. this.userLocationApi.updateLocation(7L, 116.328103,39.900835, "北京西站");
17. this.userLocationApi.updateLocation(8L, 116.609564,40.083812, "北京首都国际机场");
18. this.userLocationApi.updateLocation(9L, 116.459958,39.937193, "德云社(三里屯店)");
19. this.userLocationApi.updateLocation(10L, 116.333374,40.009645, "清华大学");
20. this.userLocationApi.updateLocation(41L, 116.316833,39.998877, "北京大学");
21. this.userLocationApi.updateLocation(42L, 117.180115,39.116464, "天津大学(卫津路校区)");
22.     }
23. }

3.2、APP接口

接口文档:https://mock-java.itheima.net/project/35/interface/api/557

3.2.1、BaiduController

1. @RestController
2. @RequestMapping("/baidu")
3. public class BaiduController {
4. 
5. @Autowired
6. private BaiduService baiduService;
7. 
8. /**
9.      * 更新位置
10.      */
11. @PostMapping("/location")
12. public ResponseEntity updateLocation(@RequestBody Map param) {
13. Double longitude = Double.valueOf(param.get("longitude").toString());
14. Double latitude = Double.valueOf(param.get("latitude").toString());
15. String address = param.get("addrStr").toString();
16. this.baiduService.updateLocation(longitude, latitude,address);
17. return ResponseEntity.ok(null);
18.     }
19. }

3.2.2、BaiduService

1. @Service
2. public class BaiduService {
3. 
4. @DubboReference
5. private UserLocationApi userLocationApi;
6. 
7. //更新地理位置
8. public void updateLocation(Double longitude, Double latitude, String address) {
9. Boolean flag = userLocationApi.updateLocation(UserHolder.getUserId(),longitude,latitude,address);
10. if(!flag) {
11. throw  new BusinessException(ErrorResult.error());
12.         }
13.     }
14. }

3.3、测试

4、搜附近

在首页中点击“搜附近”可以搜索附近的好友,效果如下:

实现思路:根据当前用户的位置,查询附近范围内的用户。范围是可以设置的。

4.1、dubbo服务

4.1.1、定义接口方法

UserLocationApi

1. /**
2.      * 根据位置搜索附近人的所有ID
3.      */
4.     List<Long> queryNearUser(Long userId, Double metre);

4.1.2、编写实现

UserLocationApiImpl  

1. @Override
2. public List<Long> queryNearUser(Long userId, Double metre) {
3. //1、根据用户id,查询用户的位置信息
4. Query query = Query.query(Criteria.where("userId").is(userId));
5. UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
6. if(location == null) {
7. return null;
8.         }
9. //2、已当前用户位置绘制原点
10. GeoJsonPoint point = location.getLocation();
11. //3、绘制半径
12. Distance distance = new Distance(metre / 1000, Metrics.KILOMETERS);
13. //4、绘制圆形
14. Circle circle = new Circle(point, distance);
15. //5、查询
16. Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
17.         List<UserLocation> list = mongoTemplate.find(locationQuery, UserLocation.class);
18. return CollUtil.getFieldValues(list,"userId",Long.class);
19.     }

4.2、APP接口服务

4.2.1、NearUserVo

1. //附近的人vo对象
2. @Data
3. @NoArgsConstructor
4. @AllArgsConstructor
5. public class NearUserVo {
6. 
7. private Long userId;
8. private String avatar;
9. private String nickname;
10. 
11. public static NearUserVo init(UserInfo userInfo) {
12. NearUserVo vo = new NearUserVo();
13.         vo.setUserId(userInfo.getId());
14.         vo.setAvatar(userInfo.getAvatar());
15.         vo.setNickname(userInfo.getNickname());
16. return vo;
17.     }
18. }

4.2.2、TanHuaController

1. /**
2.      * 搜附近
3.      */
4. @GetMapping("/search")
5. public ResponseEntity<List<NearUserVo>> queryNearUser(String gender,
6. @RequestParam(defaultValue = "2000") String distance) {
7.         List<NearUserVo> list = this.tanhuaService.queryNearUser(gender, distance);
8. return ResponseEntity.ok(list);
9.     }

4.2.3、TanHuaService

1. //搜附近
2. public List<NearUserVo> queryNearUser(String gender, String distance) {
3. //1、调用API查询附近的用户(返回的是附近的人的所有用户id,包含当前用户的id)
4.         List<Long> userIds = userLocationApi.queryNearUser(UserHolder.getUserId(),Double.valueOf(distance));
5. //2、判断集合是否为空
6. if(CollUtil.isEmpty(userIds)) {
7. return new ArrayList<>();
8.         }
9. //3、调用UserInfoApi根据用户id查询用户详情
10. UserInfo userInfo = new UserInfo();
11.         userInfo.setGender(gender);
12.         Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, userInfo);
13. //4、构造返回值。
14.         List<NearUserVo> vos = new ArrayList<>();
15. for (Long userId : userIds) {
16. //排除当前用户
17. if(userId == UserHolder.getUserId()) {
18. continue;
19.             }
20. UserInfo info = map.get(userId);
21. if(info != null) {
22. NearUserVo vo = NearUserVo.init(info);
23.                 vos.add(vo);
24.             }
25.         }
26. return vos;
27.     }

4.2.4、测试

相关文章
|
机器学习/深度学习 数据采集 算法
Machine Learning机器学习之K近邻算法(K-Nearest Neighbors,KNN)
Machine Learning机器学习之K近邻算法(K-Nearest Neighbors,KNN)
|
12月前
|
API
车牌号归属地查询免费API接口教程
本接口用于根据车牌号查询社会车辆的归属地,不支持军车、使馆等特殊车牌。请求地址为 `https://cn.apihz.cn/api/other/chepai.php`,支持 POST 和 GET 请求。请求参数包括 `id`、`key` 和 `words`,返回数据包含车牌归属地信息。示例请求:`https://cn.apihz.cn/api/other/chepai.php?id=88888888&key=88888888&words=川B1234`。
554 21
|
10月前
|
C# 开发工具 C++
code runner 运行C#项目
本文介绍了如何修改Code Runner设置使 Visual Studio Code (VS Code) 能直接运行完整的 C# 项目。传统方式依赖 cscript 工具,仅支持 .csx 文件,功能受限且已停止维护。新配置使用 `dotnet run` 命令,结合一系列炫酷的cmd指令,将指令定位到具体的csproj文件上进行运行。
469 38
|
11月前
|
JavaScript
Vite env 环境配置
Vite env 环境配置
295 4
Vite env 环境配置
|
11月前
|
机器学习/深度学习 存储 人工智能
《C++ 赋能强化学习:Q - learning 算法的实现之路》
本文探讨了如何用C++实现强化学习中的Q-learning算法。强化学习通过智能体与环境的交互来学习最优策略,Q-learning则通过更新Q函数估计动作回报。C++凭借高效的内存管理和快速执行,在处理大规模数据和复杂计算时表现出色。文章详细介绍了环境建模、Q表初始化、训练循环及策略提取等关键步骤,并分析了其在游戏开发、机器人控制等领域的应用前景,同时指出了可能面临的挑战及应对策略。
317 11
|
12月前
|
传感器 IDE 开发工具
如何在 Arduino 和 Raspberry Pi 上实现相同的功能
本文介绍了如何在Arduino和Raspberry Pi上实现相同的功能,通过对比两种平台的硬件和软件特性,帮助读者选择最适合项目的开发板,并提供实用的编程技巧和示例代码。
|
12月前
|
人工智能 算法
AI 写歌词,会让歌词创作变得更容易吗?
在科技迅猛发展的今天,AI已渗透至多个领域,包括歌词创作。《妙笔生词智能写歌词软件》通过强大算法与海量数据,为新手提供创作指导,快速生成多风格歌词片段,降低创作门槛,节省时间。尽管如此,优秀作品仍需创作者的情感与思考,AI辅助下的歌词创作正逐渐变得更为便捷。
|
移动开发 缓存 前端开发
构建高效的前端路由系统:从原理到实践
在现代Web开发中,前端路由系统已成为构建单页面应用(SPA)不可或缺的核心技术之一。不同于传统服务器渲染的多页面应用,SPA通过前端路由技术实现了页面的局部刷新与无缝导航,极大地提升了用户体验。本文将深入剖析前端路由的工作原理,包括Hash模式与History模式的实现差异,并通过实战演示如何在Vue.js框架中构建一个高效、可维护的前端路由系统。我们还将探讨如何优化路由加载性能,确保应用在不同网络环境下的流畅运行。本文不仅适合前端开发者深入了解前端路由的奥秘,也为后端转前端或初学者提供了从零到一的实战指南。
|
JSON API 数据格式
App Inventor 2 天气预报App开发 - 第三方API接入的通用方法
通过调用第三方天气api,填入必要的参数,通过Web客户端请求url。返回json格式的数据结果,使用AppInventor2解析json结果,显示到App上即可。
420 5
|
数据管理 数据处理 数据库
数据库中的 ACID 属性详解
【8月更文挑战第31天】
914 0
下一篇
开通oss服务