附近的人
作者:互联网
定义用户位置类:
MongoDB的 2dsphere
索引支持查询在一个类地球的球面上进行几何计算,以GeoJSON对象或者普通坐标对的方式存储数据。
@Data @NoArgsConstructor @AllArgsConstructor @Document(collection = "user_location") @CompoundIndex(name = "location_index", def = "{'location': '2dsphere'}") public class UserLocation implements java.io.Serializable{ private static final long serialVersionUID = 4508868382007529970L; @Id private ObjectId id; @Indexed private Long userId; //用户id private GeoJsonPoint location; //x:经度 y:纬度 private String address; //位置描述 private Long created; //创建时间 private Long updated; //更新时间 private Long lastUpdated; //上次更新时间 }
业务一:上报地理位置
前端会传递用户地理位置信息
/** * 上报地理位置 * @param map * @return */ @PostMapping("/location") public ResponseEntity saveLoca(@RequestBody Map map){ Double longitude =Double.valueOf(map.get("longitude").toString()) ; Double latitude = Double.valueOf(map.get("latitude").toString()); String addrStr = map.get("addrStr").toString(); baiduService.update(longitude,latitude,addrStr); return ResponseEntity.ok(null); }
保存信息到mongoDB数据库
@Override public boolean saveLoca(Double longitude, Double latitude, String addrStr, Long userId) { try { // 查询用户地址 Query query = Query.query(Criteria.where("userId").is(userId)); UserLocation location = mongoTemplate.findOne(query, UserLocation.class); // 进行判断 if (location != null) { // 更新 Update update = Update.update("location", new GeoJsonPoint(longitude, latitude)) .set("address", addrStr).set("lastUpdated", location.getUpdated()) .set("updated", System.currentTimeMillis()); mongoTemplate.updateFirst(query, update, UserLocation.class); } else { // 保存 location = new UserLocation(); location.setAddress(addrStr); location.setCreated(System.currentTimeMillis()); location.setLastUpdated(System.currentTimeMillis()); location.setUpdated(System.currentTimeMillis()); location.setUserId(userId); location.setLocation(new GeoJsonPoint(longitude, latitude)); mongoTemplate.save(location); } return true; } catch (Exception exception) { exception.printStackTrace(); return false; } }
业务二:查询附近的人
根据传递过来的信息进行查询
/** * 获取周边陌生人 * @param gender * @param distance * @return */ @GetMapping("/search") public ResponseEntity search(String gender, @RequestParam(defaultValue = "1000") String distance){ List<NearUserVo> nearUserVos = tanhuaService.queryNear(gender, distance); return ResponseEntity.ok(nearUserVos); }
根据条件进行查询
/** * 查询周围人 * @param distance * @return */ @Override public List<Long> query(String distance, Long userId) { // 获取当前用户位置 Query query = Query.query(Criteria.where("userId").is(userId)); UserLocation location = mongoTemplate.findOne(query, UserLocation.class); // 按条件查询周边 if (location==null){ return null; } // 设置圆心 GeoJsonPoint point = location.getLocation(); // 设置半径 Distance r = new Distance(Double.valueOf(distance) / 1000, Metrics.KILOMETERS); // 画圆 Circle circle = new Circle(point, r); // 查询 Query qu2 = Query.query(Criteria.where("location").withinSphere(circle)); List<UserLocation> list = mongoTemplate.find(qu2, UserLocation.class); // 返回id集合 return CollUtil.getFieldValues(list, "userId", Long.class); }
标签:return,userId,private,UserLocation,location,query,附近 来源: https://www.cnblogs.com/qaqz/p/16219392.html