MongoTemplate地理位置查詢(標準)
阿新 • • 發佈:2018-12-04
@GeoSpatialIndexed(type=GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint loc;
//GeoJsonPoint loc = new GeoJsonPoint(lon, lat);
- 矩形查詢
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);
- 圓形查詢(米)
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);
- 最近點查詢
Point p = new Point(lon, lat); Query query = new Query(Criteria.where("loc").nearSphere(p)); return mongoTemplate.find(query.limit(size), LonLat.class);