MongoTemplate地理位置查询(标准)

@GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint loc;
//GeoJsonPoint loc = new GeoJsonPoint(lon, lat);
  1. 矩形查询
		Point bottomLeft = new Point(minLon, minLat);
		Point topRight = new Point(maxLon, maxLat);
		Box box = new Box(bottomLeft, topRight);
		Query query = new Query(Criteria.where("loc").within(box));
		return mongoTemplate.find(query, LonLat.class);
  1. 圆形查询(米)
      Point center = new Point(lon, lat);
      Circle circle = new Circle(center, new Distance(distance / 1000D, Metrics.KILOMETERS));
      query.addCriteria(Criteria.where("loc").withinSphere(circle));
      return mongoTemplate.find(query, LonLat.class);
  1. 最近点查询
		Point p = new Point(lon, lat);
		Query query = new Query(Criteria.where("loc").nearSphere(p));
		return mongoTemplate.find(query.limit(size), LonLat.class);

猜你喜欢

转载自blog.csdn.net/jiangshuanshuan/article/details/84567070
今日推荐