| | |
| | | |
| | | import com.xiaopeng.api.vo.automaticPilot.VehiclePosition; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.data.geo.*; |
| | | import org.springframework.data.geo.Circle; |
| | | import org.springframework.data.geo.Distance; |
| | | import org.springframework.data.geo.Metrics; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.geo.GeoJsonPoint; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.NearQuery; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class VehiclePositionService { |
| | |
| | | * 查询附近指定距离内且状态正常的车辆 |
| | | * @param longitude 经度 |
| | | * @param latitude 纬度 |
| | | * @param distanceInMeters 距离(米) |
| | | * @param distanceInMeters 距离(KM) |
| | | * @return 符合条件的车辆列表 |
| | | */ |
| | | public List<VehiclePosition> findAvailableVehiclesNearby(double longitude, |
| | |
| | | // .map(geoResult -> geoResult.getContent()) |
| | | // .collect(Collectors.toList()); |
| | | |
| | | //找到中心点 |
| | | // 找到中心点 |
| | | GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude); |
| | | //构造半径 |
| | | Distance distanceR = new Distance(distanceInMeters / 1000, Metrics.KILOMETERS); |
| | | //画圆 |
| | | // 构造半径 |
| | | Distance distanceR = new Distance(distanceInMeters, Metrics.KILOMETERS); |
| | | // 画圆 |
| | | Circle circle = new Circle(geoJsonPoint, distanceR); |
| | | // 构造query对象 |
| | | Query query = Query.query(Criteria.where("point").withinSphere(circle)); |
| | | long latestAllowedAddTime = System.currentTimeMillis() - 60 * 1000; |
| | | Query query = Query.query(new Criteria().andOperator( |
| | | Criteria.where("point").withinSphere(circle), |
| | | Criteria.where("addTime").gte(latestAllowedAddTime) |
| | | )); |
| | | List<VehiclePosition> vehiclePositions = mongoTemplate.find(query, VehiclePosition.class); |
| | | return vehiclePositions; |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 根据车辆ID查询车辆信息 |
| | | * @param vehicleId 车辆ID |