mongodb-mongotemplate进行地理坐标操作

因为项目中使用的springboot + mongotemplate, 所以还是需要mongotemplate的操作方式

首先建立一个bean: 

package com.iwhere.easy.travel.entity;

import java.io.Serializable;
import java.util.Arrays;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 收费poi
 * 
 * @author wenbronk
 * @time 2017年7月19日 下午4:46:39
 */

@Document(collection = "charge_poi")
public class ChargePoi implements Serializable {
    private static final long serialVersionUID = 2653147280472201924L;

    @Id
    private String _id;

    @Indexed
    private String poi_id;
    private String poi_name;
    @GeoSpatialIndexed
    private Double[] location;
    private String media_url;
    private Double price;

    public ChargePoi() {
        super();
    }

    @PersistenceConstructor
    ChargePoi(String _id, String poi_id, String poi_name, Double[] location, String media_url, Double price) {
        super();
        this._id = _id;
        this.poi_id = poi_id;
        this.poi_name = poi_name;
        this.location = location;
        this.media_url = media_url;
        this.price = price;
    }

    public ChargePoi(String _id, String poi_id, String poi_name, Double x, Double y, String media_url, Double price) {
        super();
        this._id = _id;
        this.poi_id = poi_id;
        this.poi_name = poi_name;
        this.location = new Double[]{x, y};
        this.media_url = media_url;
        this.price = price;
    }

    public String get_id() {
        return _id;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public String getPoi_id() {
        return poi_id;
    }

    public void setPoi_id(String poi_id) {
        this.poi_id = poi_id;
    }

    public String getPoi_name() {
        return poi_name;
    }

    public void setPoi_name(String poi_name) {
        this.poi_name = poi_name;
    }

    public Double[] getLocation() {
        return location;
    }

    public void setLocation(Double[] location) {
        this.location = location;
    }

    public String getMedia_url() {
        return media_url;
    }

    public void setMedia_url(String media_url) {
        this.media_url = media_url;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "ChargePoi [_id=" + _id + ", poi_id=" + poi_id + ", poi_name=" + poi_name + ", location="
                + Arrays.toString(location) + ", media_url=" + media_url + ", price=" + price + "]";
    }
}

注: 

@GeoSpatialIndexed  注解的作用: 

Mark a field to be indexed using MongoDB's geospatial indexing feature.
开始没有加这个注解, 然后计算距离的时候就会报错

1, 准备测试数据: 

    /**
     * save
     */
    @Test
    public void test1() {
        for (int x = 100; x < 131; x++) {
            for (int y = 30; y < 61; y++) {
                Double loca[] = new Double[]{Double.valueOf(x), Double.valueOf(y)};
                ChargePoi chargePoi = new ChargePoi();
                chargePoi.setPoi_id("poiid" + x);
                chargePoi.setMedia_url("http://www.baidu.com?params=" + x);
                chargePoi.setPoi_name("vini" + Arrays.toString(loca));
                chargePoi.setPrice(Math.random() * 100);
                chargePoi.setLocation(loca);
                mongoTemplate.insert(chargePoi);
            }
        }
    }

2, 圆形查询

    /**
     * circle
     */
    @Test
    public void test2() {
        Circle circle = new Circle(30, 20, 20);
        List<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").within(circle)), ChargePoi.class);
        System.out.println(find);
        System.out.println(find.size());
    }

3, 球星查询

    /**
     * spherical 
     */
    @Test
    public void test3() {
        Circle circle = new Circle(30,20, 20);
        List<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").withinSphere(circle)), ChargePoi.class);
        System.out.println(find.size());
        System.out.println(find);
    }

4, 矩形查询, box

    /**
     * box
     */
    @Test
    public void test4() {
        Box box = new Box(new Point(10, 11), new Point(10, 20));  
        List<ChargePoi> find = 
                mongoTemplate.find(new Query(Criteria.where("location").within(box)), ChargePoi.class);
        System.out.println(find.size());
        System.out.println(find);
    }

5, 按距离由近到元查询

    /**
     * near
     */
    @Test
    public void test5() {
        Point point = new Point(12, 12);
        List<ChargePoi> venues = 
                mongoTemplate.find(new Query(Criteria.where("location").near(point).maxDistance(20)), ChargePoi.class);
        System.out.println(venues.size());
        System.out.println(venues);
    }

6, 空间距离查询

    /**
     * nearSphere
     */
    @Test
    public void test6() {
        Point point = new Point(12, 12);
        List<ChargePoi> venues = 
                mongoTemplate.find(new Query(Criteria.where("location").nearSphere(point).maxDistance(20)), ChargePoi.class);
        System.out.println(venues.size());
        System.out.println(venues);
    }

7, 最近点查询

    @Test
    public void test7() {
        Point location = new Point(12, 12);
        NearQuery query = NearQuery.near(location).maxDistance(new Distance(100000, Metrics.KILOMETERS));
        GeoResults<ChargePoi> result = mongoTemplate.geoNear(query, ChargePoi.class);
        System.out.println(result);
    }

 

我是勤劳的搬运工:->http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.RELEASE/reference/html/mongo.core.html#mongo.geospatial

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值