当前位置:   article > 正文

mongoDB地理位置检索_mongotemplate.geonear

mongotemplate.geonear

查询附近

  1. @Test
  2. public void testNear() {
  3. //构造坐标点
  4. GeoJsonPoint point = new GeoJsonPoint(116.404, 39.915);
  5. //构造半径
  6. Distance distanceObj = new Distance(1, Metrics.KILOMETERS);
  7. //画了一个圆圈
  8. Circle circle = new Circle(point, distanceObj);
  9. //构造query对象
  10. Query query = Query.query(Criteria.where("location").withinSphere(circle));
  11. List<Places> list = mongoTemplate.find(query, Places.class);
  12. list.forEach(System.out::println);
  13. }

查询并获取距离

我们假设需要以当前坐标为原点,查询附近指定范围内的餐厅,并直接显示距离

  1. //查询附近且获取间距
  2. @Test
  3. public void testNear1() {
  4. //1、构造中心点(圆点)
  5. GeoJsonPoint point = new GeoJsonPoint(116.404, 39.915);
  6. //2、构建NearQuery对象
  7. NearQuery query = NearQuery.near(point, Metrics.KILOMETERS).maxDistance(1, Metrics.KILOMETERS);
  8. //3、调用mongoTemplate的geoNear方法查询
  9. GeoResults<Places> results = mongoTemplate.geoNear(query, Places.class);
  10. //4、解析G
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/503986
推荐阅读
相关标签
  

闽ICP备14008679号