SpringBoot项目使用MongoDB距离查询

  • 数据库
    在这里插入图片描述

  • 实体类

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");
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值