@Override
public Double queryDistanceByTwoPoint(String userId, Double[] coordinate) {
/*指定的一个点*/
Point point = new Point(coordinate[0], coordinate[1]);
/*查询出另外一个点,userId和onlineState组合起来查询出唯一一条记录*/
Criteria criteria = Criteria.where("userId").is(userId)
.and("onlineState").is(0x02);
Query query = new Query(criteria);
NearQuery nearQuery = NearQuery.near(point, Metrics.KILOMETERS).query(query);
logger.debug("查询两点间的距离 {}", nearQuery.toDBObject().toString());
GeoResults<LocationInfo> geoResults = mongoTemplate.geoNear(nearQuery, LocationInfo.class);
Iterator<GeoResult<LocationInfo>> iterator = geoResults.iterator();
while (iterator.hasNext()) {
GeoResult<LocationInfo> geoResult = iterator.next();
double distance = geoResult.getDistance().getValue();
return distance;
}
return null;
}
转载于:https://my.oschina.net/linwenbin/blog/420052