SpringBoot项目使用MongoDB距离查询

SpringBoot项目使用MongoDB距离查询,第1张

  • 数据库

  • 实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * @program: tanhua
 * @ClassName Places
 * @description:
 * @author: xuewen
 * @create: 2022-05-02 15:46
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document("places")
@CompoundIndex(name = "location_index",def = "{'location'}:{'2dsphere'}")
public class Places {
    private ObjectId id;
    private String title;
    private String address;
    private GeoJsonPoint location;
}

  • 测试
import com.czxy.domain.Places;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @program: tanhua
 * @ClassName Test
 * @description:
 * @author: xuewen
 * @create: 2022-05-02 15:53
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoDemoApplication.class)
public class Test {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * @description 查询xx千米内的地点
     * @params []
     * @author xuewen
     * @date 2022/5/2 16:02
     * @return void
    */
    @org.junit.Test
    public void  test01(){
        //构造坐标点
        GeoJsonPoint point = new GeoJsonPoint(116.40, 39.91); //天安门坐标
        //构造半径
        Distance distanceObj = new Distance(1, Metrics.KILOMETERS);
        // 画了一个圆圈
        Circle circle = new Circle(point, distanceObj);
        // 构造query对象
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        // 省略其他内容
        List<Places> list = mongoTemplate.find(query, Places.class);
        list.forEach(System.out::println);
    }
    /**
     * @description 查询xx千米内的地点并计算距离
     * @author xuewen
     * @date 2022/5/2 16:03
     * @return void
    */
    @org.junit.Test
    public void testGeoNear() {
        //构建中心点
        GeoJsonPoint point = new GeoJsonPoint(116.404, 39.915);
        //创建NearQuery对象
        NearQuery nearQuery = NearQuery.near(point,Metrics.KILOMETERS).maxDistance(1,Metrics.KILOMETERS); //maxDistance 最大距离
        //发送查询
        GeoResults<Places> results = mongoTemplate.geoNear(nearQuery, Places.class);
        for (GeoResult<Places> result : results) {
            System.out.println(result.getContent());
            System.out.println("距离"+result.getDistance().getValue()+"km");
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/798559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存