因为项目中使用的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 = ; x < ; x++) {
for (int y = ; y < ; 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() * );
chargePoi.setLocation(loca);
mongoTemplate.insert(chargePoi);
}
}
}

2, 圆形查询

    /**
* circle
*/
@Test
public void test2() {
Circle circle = new Circle(, , );
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(,, );
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(, ), new Point(, ));
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(, );
List<ChargePoi> venues =
mongoTemplate.find(new Query(Criteria.where("location").near(point).maxDistance()), ChargePoi.class);
System.out.println(venues.size());
System.out.println(venues);
}

6, 空间距离查询

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

7, 最近点查询

    @Test
public void test7() {
Point location = new Point(, );
NearQuery query = NearQuery.near(location).maxDistance(new Distance(, 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

												

mongodb-mongotemplate进行地理坐标操作的更多相关文章

  1. Node.js 中MongoDB的基本接口操作

    Node.js 中MongoDB的基本接口操作 连接数据库 安装mongodb模块 导入mongodb模块 调用connect方法 文档的增删改查操作 插入文档 方法: db.collection(& ...

  2. MongoDB之三(高级操作 聚合、游标)

    一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...

  3. mysql数据库和mongodb数据库的相关操作以及两个数据库的区别

    在docs命令中执行数据操作 MySQL数据库 先启动MySQL服务器  net start mysql 进入MySQL服务器MySQL -uroot -p(这里写你的数据库密码) (-P是从哪个端口 ...

  4. 数据库【mongodb篇】练习操作

    本文的目标是通过大量的示例,来更好的理解如果在Mongodb中进行数据操作: 初入客户端刚利用 mongod命令进入客户端环境,此时对数据库一无所知: 举目四望,想知道现在有哪些数据库,   show ...

  5. MongoDB入门 和nodejs操作

    简介 MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshel ...

  6. 第一篇:一天学会MongoDB数据库之Python操作

    本文仅仅学习使用,转自:https://www.cnblogs.com/suoning/p/6759367.html#3682005 里面新增了如果用用Python代码进行增删改查 什么是MongoD ...

  7. mongodb常用语句(集合操作)

    mongodb常用语句(集合操作) 查看集合帮助 db.songs.help(); 查看集合总数据量 db.songs.count(); 查看表空间大小 db.songs.dataSize(); 查看 ...

  8. MongoDB API和python操作

    安装 下载mongodb的版本,两点注意 根据业界规则,偶数为稳定版,如1.6.X,奇数为开发版,如1.7.X 32bit的mongodb最大只能存放2G的数据,64bit就没有限制 到官网,选择合适 ...

  9. python数据库-mongoDB的高级查询操作(55)

    一.MongoDB索引 为什么使用索引? 假设有一本书,你想看第六章第六节讲的是什么,你会怎么做,一般人肯定去看目录,找到这一节对应的页数,然后翻到这一页.这就是目录索引,帮助读者快速找到想要的章节. ...

  10. MongoDB学习笔记:Python 操作MongoDB

    MongoDB学习笔记:Python 操作MongoDB   Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...

随机推荐

  1. hdu 5023 线段树+状压

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...

  2. 第一章 JVM内存结构

    注意:本系列博客,主要参考自以下四本书 <分布式Java应用:基础与实践><深入理解Java虚拟机(第二版)><深入分析Java web技术内幕><实战jav ...

  3. aop的概述

    支付部分,定义IPayService接口并定义支付方法“pay”,并定义了两个实现:“PointPayService”表示积分支付,“RMBPayService”表示人民币支付:并且在每个支付实现中支 ...

  4. GitLab使用自定义端口

      Git支持两种地址访问方式,一种是:使用ssh协议,另一种是:使用http协议.   今天在部署Git服务器拉取和上传代码是出现了以下问题ssh: connect to host gitlab.d ...

  5. Eclipse ADT 与VS 常用的快捷键 对比学习

    注:以下说的类型于VS,是指:VS+Resharper的快捷键,我是采用了Resharper作为VS的快捷键. 导航 Ctrl+1 快速修复 (类似于VS的alt+enter) Ctrl+D: 删除当 ...

  6. (zxing.net)一维码MSI的简介、实现与解码

    一.简介 MSI/Plessey 条码(也被称为 MSI 或 Modified Plessey)是一款数字条码,多用于超市.存储用的仓库和其他贮藏室的货架.货架上的条码可以告知货架上的产品.应放数量和 ...

  7. Asp.Net Mvc ScriptBundle 脚本文件捆绑压缩 导致 脚本出错的问题

    由于捆绑压缩会对所有包含的文件进行压缩,无法设置忽略对某个js文件的压缩.导致压缩该js后,脚本出错的问题. 解决方式: 重写 ScriptBundle 的 GenerateBundleRespons ...

  8. .net推送微信消息模板

    1.获取access_token public string GetAccess_Token() { string appid = WxPayConfig.APPID; string appsecre ...

  9. 网易郑栋:数据采集与分析的那些事——从数据埋点到AB测试

    本文由  网易云发布. 4月8日晚,DTalk邀请到了网易互联网分析产品.可视化 BI 产品的负责人—郑栋老师,进行了一次关于<网易郑栋:数据采集与分析的那些事第一弹: 数据篇>的主题分享 ...

  10. 《Python黑帽子:黑客与渗透测试编程之道》 Scapy:网络的掌控者

    窃取email认证: 测试代码: #!/usr/bin/python #coding=utf-8 from scapy.all import * #数据包回调函数 def packet_callbac ...