puzhibing
2026-03-04 a6f0b69caf5c1913b6fec8b94d2454b33d15d649
xp-robotaxi-operation-boot/src/main/java/com/xiaopeng/operation/service/impl/XpGeoQueryEntityServiceImpl.java
@@ -5,11 +5,9 @@
import com.xiaopeng.operation.repository.XpGeoQueryEntityRepository;
import com.xiaopeng.operation.service.IXpGeoQueryEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.*;
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;
@@ -52,37 +50,48 @@
        calendar.add(Calendar.DAY_OF_YEAR, -7);
        Long startTime = calendar.getTime().getTime();
        // 1. 创建查询点
        Point referencePoint = new Point(longitude, latitude);
        // 2. 构建 NearQuery - 完全使用 NearQuery 的 API
        NearQuery nearQuery = NearQuery.near(referencePoint, Metrics.KILOMETERS)
                .maxDistance(0.5)  // 转成公里
                .spherical(true);
        // 3.时间>startTime time = time参数
        // 3. 创建额外的查询条件
        Query query = new Query();
//        // 1. 创建查询点
//        Point referencePoint = new Point(longitude, latitude);
//
//        // 2. 构建 NearQuery - 完全使用 NearQuery 的 API
//        NearQuery nearQuery = NearQuery.near(referencePoint, Metrics.KILOMETERS)
//                .maxDistance(0.5)  // 转成公里
//                .spherical(true);
//
//
//        // 3.时间>startTime time = time参数
//        // 3. 创建额外的查询条件
//        Query query = new Query();
//        query.addCriteria(Criteria.where("timestamp").gte(startTime));
//        query.addCriteria(Criteria.where("time").is(time));
//
//        // 将 NearQuery 和额外的查询条件合并
//        nearQuery.query(query);
//
//
//        // 4. 执行查询 - 明确指定集合名
//        GeoResults<XpGeoQueryEntity> geoResults = mongoTemplate.geoNear(
//                nearQuery,
//                XpGeoQueryEntity.class,
//                "xp_geo_query_entity"  // 明确指定集合名,避免映射错误
//        );
//
//        // 5. 提取结果
//        return geoResults.getContent().stream()
//                .map(geoResult -> geoResult.getContent())
//                .collect(Collectors.toList());
        //找到中心点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude);
        //构造半径
        Distance distanceR = new Distance(0.5, Metrics.KILOMETERS);
        //画圆
        Circle circle = new Circle(geoJsonPoint, distanceR);
        // 构造query对象
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        query.addCriteria(Criteria.where("timestamp").gte(startTime));
        query.addCriteria(Criteria.where("time").is(time));
        // 将 NearQuery 和额外的查询条件合并
        nearQuery.query(query);
        // 4. 执行查询 - 明确指定集合名
        GeoResults<XpGeoQueryEntity> geoResults = mongoTemplate.geoNear(
                nearQuery,
                XpGeoQueryEntity.class,
                "xp_geo_query_entity"  // 明确指定集合名,避免映射错误
        );
        // 5. 提取结果
        return geoResults.getContent().stream()
                .map(geoResult -> geoResult.getContent())
                .collect(Collectors.toList());
        List<XpGeoQueryEntity> xpGeoQueryEntities = mongoTemplate.find(query, XpGeoQueryEntity.class);
        return xpGeoQueryEntities;
    }
    /**