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

首先建立一个bean:

  1. package com.iwhere.easy.travel.entity;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Arrays;
  5.  
  6. import org.springframework.data.annotation.Id;
  7. import org.springframework.data.annotation.PersistenceConstructor;
  8. import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
  9. import org.springframework.data.mongodb.core.index.Indexed;
  10. import org.springframework.data.mongodb.core.mapping.Document;
  11.  
  12. /**
  13. * 收费poi
  14. *
  15. * @author wenbronk
  16. * @time 2017年7月19日 下午4:46:39
  17. */
  18.  
  19. @Document(collection = "charge_poi")
  20. public class ChargePoi implements Serializable {
  21. private static final long serialVersionUID = 2653147280472201924L;
  22.  
  23. @Id
  24. private String _id;
  25.  
  26. @Indexed
  27. private String poi_id;
  28. private String poi_name;
  29. @GeoSpatialIndexed
  30. private Double[] location;
  31. private String media_url;
  32. private Double price;
  33.  
  34. public ChargePoi() {
  35. super();
  36. }
  37.  
  38. @PersistenceConstructor
  39. ChargePoi(String _id, String poi_id, String poi_name, Double[] location, String media_url, Double price) {
  40. super();
  41. this._id = _id;
  42. this.poi_id = poi_id;
  43. this.poi_name = poi_name;
  44. this.location = location;
  45. this.media_url = media_url;
  46. this.price = price;
  47. }
  48.  
  49. public ChargePoi(String _id, String poi_id, String poi_name, Double x, Double y, String media_url, Double price) {
  50. super();
  51. this._id = _id;
  52. this.poi_id = poi_id;
  53. this.poi_name = poi_name;
  54. this.location = new Double[]{x, y};
  55. this.media_url = media_url;
  56. this.price = price;
  57. }
  58.  
  59. public String get_id() {
  60. return _id;
  61. }
  62.  
  63. public void set_id(String _id) {
  64. this._id = _id;
  65. }
  66.  
  67. public String getPoi_id() {
  68. return poi_id;
  69. }
  70.  
  71. public void setPoi_id(String poi_id) {
  72. this.poi_id = poi_id;
  73. }
  74.  
  75. public String getPoi_name() {
  76. return poi_name;
  77. }
  78.  
  79. public void setPoi_name(String poi_name) {
  80. this.poi_name = poi_name;
  81. }
  82.  
  83. public Double[] getLocation() {
  84. return location;
  85. }
  86.  
  87. public void setLocation(Double[] location) {
  88. this.location = location;
  89. }
  90.  
  91. public String getMedia_url() {
  92. return media_url;
  93. }
  94.  
  95. public void setMedia_url(String media_url) {
  96. this.media_url = media_url;
  97. }
  98.  
  99. public Double getPrice() {
  100. return price;
  101. }
  102.  
  103. public void setPrice(Double price) {
  104. this.price = price;
  105. }
  106.  
  107. @Override
  108. public String toString() {
  109. return "ChargePoi [_id=" + _id + ", poi_id=" + poi_id + ", poi_name=" + poi_name + ", location="
  110. + Arrays.toString(location) + ", media_url=" + media_url + ", price=" + price + "]";
  111. }
  112. }

注:

  1. @GeoSpatialIndexed 注解的作用:

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

1, 准备测试数据:

  1. /**
  2. * save
  3. */
  4. @Test
  5. public void test1() {
  6. for (int x = ; x < ; x++) {
  7. for (int y = ; y < ; y++) {
  8. Double loca[] = new Double[]{Double.valueOf(x), Double.valueOf(y)};
  9. ChargePoi chargePoi = new ChargePoi();
  10. chargePoi.setPoi_id("poiid" + x);
  11. chargePoi.setMedia_url("http://www.baidu.com?params=" + x);
  12. chargePoi.setPoi_name("vini" + Arrays.toString(loca));
  13. chargePoi.setPrice(Math.random() * );
  14. chargePoi.setLocation(loca);
  15. mongoTemplate.insert(chargePoi);
  16. }
  17. }
  18. }

2, 圆形查询

  1. /**
  2. * circle
  3. */
  4. @Test
  5. public void test2() {
  6. Circle circle = new Circle(, , );
  7. List<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").within(circle)), ChargePoi.class);
  8. System.out.println(find);
  9. System.out.println(find.size());
  10. }

3, 球星查询

  1. /**
  2. * spherical
  3. */
  4. @Test
  5. public void test3() {
  6. Circle circle = new Circle(,, );
  7. List<ChargePoi> find = mongoTemplate.find(new Query(Criteria.where("location").withinSphere(circle)), ChargePoi.class);
  8. System.out.println(find.size());
  9. System.out.println(find);
  10. }

4, 矩形查询, box

  1. /**
  2. * box
  3. */
  4. @Test
  5. public void test4() {
  6. Box box = new Box(new Point(, ), new Point(, ));
  7. List<ChargePoi> find =
  8. mongoTemplate.find(new Query(Criteria.where("location").within(box)), ChargePoi.class);
  9. System.out.println(find.size());
  10. System.out.println(find);
  11. }

5, 按距离由近到元查询

  1. /**
  2. * near
  3. */
  4. @Test
  5. public void test5() {
  6. Point point = new Point(, );
  7. List<ChargePoi> venues =
  8. mongoTemplate.find(new Query(Criteria.where("location").near(point).maxDistance()), ChargePoi.class);
  9. System.out.println(venues.size());
  10. System.out.println(venues);
  11. }

6, 空间距离查询

  1. /**
  2. * nearSphere
  3. */
  4. @Test
  5. public void test6() {
  6. Point point = new Point(, );
  7. List<ChargePoi> venues =
  8. mongoTemplate.find(new Query(Criteria.where("location").nearSphere(point).maxDistance()), ChargePoi.class);
  9. System.out.println(venues.size());
  10. System.out.println(venues);
  11. }

7, 最近点查询

  1. @Test
  2. public void test7() {
  3. Point location = new Point(, );
  4. NearQuery query = NearQuery.near(location).maxDistance(new Distance(, Metrics.KILOMETERS));
  5. GeoResults<ChargePoi> result = mongoTemplate.geoNear(query, ChargePoi.class);
  6. System.out.println(result);
  7. }

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

  1.  

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. 解决 multiple definition of

    总结了解决multiple definition of的方法: 问题原因:    当多个文件包含同一个头文件时,并且你的.H里面没有加上条件编译#ifndef TEST_H#define TEST_H ...

  2. Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox组件来从列表中选择某一项

    http://blog.csdn.net/delphiteacher/article/details/8924110 Delphi for iOS开发指南(6):在iOS应用程序中使用ComboBox ...

  3. matlab 降维工具箱

    Matlab Toolbox for Dimensionality Reduction   降维方法包括: Principal Component Analysis (PCA) • Probabili ...

  4. 自适应XAML布局经验总结 (一)原则和页面结构设计

    XAML布局回顾 Grid和StackPanel是核心布局,尤其以Grid最为重要. Grid是网格布局,XAML的设计者有可能参考了Html里的Table设计了Grid布局,但进行了改进.Html中 ...

  5. Django报错:ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。

    ajax请求时加上 async : false, $.ajax({ url:"{% url 'article:article_post' %}", {#一定不要写成小写了,坑了好久 ...

  6. PHP/ThinkPHP5 框架集成微博登录入库流程示意

    PHP/ThinkPHP5 框架集成微博登录入库流程示意 第三方登陆这个东东,目前主要是 微信.微博.qq.淘宝.支付宝 等几个.他们都是基于oath2协议的.原理差不多.这里记录的是我测试的新郎微博 ...

  7. C#八大排序算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. day52 进程与守护进程

    http://www.cnblogs.com/Eva-J/articles/8253549.html 博客参考. 多进程聊天 守护进程. 多进程 1.Unix/Linux:fork()调用实现多进程. ...

  9. 使用git在gitlab上拉取代码的方法

    最近在项目中用到了gitlab,他是一个类似于github的代码托管工具. 因为是第一次使用还不太熟悉,所以在此记录一下. 1.首先需要使用github的注册账号登录gitlab,查看右上角用户头像处 ...

  10. MySQL(外键变种)

    day58 外键的变种                  a. 用户表和部门表                      用户:        不唯一                1 alex    ...