MongoDB地理位置检索
一、查询当前坐标附近的目标
@Test
public void queryNear(){
GeoJsonPoint point = new GeoJsonPoint(116.404, 39.915);
Distance distance = new Distance(1,Metrics.KILOMETERS);
Circle circle = new Circle(point, distance);
Criteria criteria = Criteria.where("location").withinSphere(circle);
Query query = Query.query(criteria);
List<Places> places = mongoTemplate.find(query, Places.class);
for (Places place : places) {
System.out.println(place);
}
}
二、查询并获取距离
@Test
public void queryNearAndGetDistance() {
GeoJsonPoint geoJsonPoint = new GeoJsonPoint(116.404, 39.915);
NearQuery nearQuery = NearQuery.near(geoJsonPoint, Metrics.KILOMETERS)
.maxDistance(1, Metrics.KILOMETERS);
GeoResults<Places> places = mongoTemplate.geoNear(nearQuery, Places.class);
for (GeoResult<Places> place : places) {
@NonNull Places content = place.getContent();
String address = content.getAddress();
double value = place.getDistance().getValue();
System