1. 程式人生 > >MongoTemplate地理位置查詢(標準)

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);