Because of the Springboot + mongotemplate used in the project, it is still necessary to mongotemplate operation mode
First create a 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;/** * Fee POI * * @author Wenbronk * @time July 19, 2017 pm 4:46:39*/@Document (Collection="Charge_poi") Public classChargepoi implements Serializable {Private StaticFinalLongSerialversionuid =2653147280472201924L; @IdPrivateString _id; @IndexedPrivateString poi_id; PrivateString Poi_name; @GeoSpatialIndexedPrivatedouble[] location; PrivateString Media_url; PrivateDouble Price; PublicChargepoi () {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 ; } PublicChargepoi (String _id, String poi_id, String poi_name, Double x, double y, String media_url, double price) {su Per (); This. _id =_id; This. poi_id =poi_id; This. Poi_name =Poi_name; This. location =Newdouble[]{x, y}; This. Media_url =Media_url; This. Price =Price ; } PublicString get_id () {return_id; } Public voidset_id (String _id) { This. _id =_id; } PublicString getpoi_id () {returnpoi_id; } Public voidsetpoi_id (String poi_id) { This. poi_id =poi_id; } PublicString Getpoi_name () {returnPoi_name; } Public voidsetpoi_name (String poi_name) { This. Poi_name =Poi_name; } Publicdouble[] GetLocation () {returnLocation ; } Public voidsetlocation (double[] location) { This. location =Location ; } PublicString Getmedia_url () {returnMedia_url; } Public voidSetmedia_url (String media_url) { This. Media_url =Media_url; } PublicDouble GetPrice () {returnPrice ; } Public voidSetprice (Double price) { This. Price =Price ; } @Override PublicString toString () {return "Chargepoi [_id="+ _id +", poi_id="+ poi_id +", Poi_name="+ Poi_name +", location="+ arrays.tostring (location) +", Media_url="+ Media_url +", price="+ Price +"]"; }}
Note:
@GeoSpatialIndexed
using MongoDB'S Geospatial indexing feature.
Start without adding this annotation, and then calculate the distance when the error will be
1, prepare the test data:
/** * Save*/@Test Public voidtest1 () { for(intx = -; X <131; X + +) { for(inty = -; Y < A; y++) {Double loca[]=Newdouble[]{double.valueof (x), double.valueof (y)}; Chargepoi Chargepoi=NewChargepoi (); 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, circular query
/** * Circle*/@Test Public voidtest2 () {Circle Circle=NewCircle ( -, -, -); List<ChargePoi> find = Mongotemplate.find (NewQuery (Criteria).where(" Location"). Within (circle)), Chargepoi.class); System. out. println (Find); System. out. println (Find.size ()); }
3, Star Search
/** * Spherical*/@Test Public voidtest3 () {Circle Circle=NewCircle ( -, -, -); List<ChargePoi> find = Mongotemplate.find (NewQuery (Criteria).where(" Location"). Withinsphere (Circle)), Chargepoi.class); System. out. println (Find.size ()); System. out. println (Find); }
4, Rectangle query, Box
/** * Box*/@Test Public voidtest4 () {Box box=NewBox (NewPoint (Ten, One),NewPoint (Ten, -)); List<ChargePoi> find =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Within (box), Chargepoi.class); System. out. println (Find.size ()); System. out. println (Find); }
5, by distance from near to Meta query
/** * near*/@Test Public voidTest5 () { point point=NewPoint ( A, A); List<ChargePoi> venues =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Near (point). MaxDistance ( -)), Chargepoi.class); System. out. println (Venues.size ()); System. out. println (venues); }
6, Spatial distance query
/** * Nearsphere*/@Test Public voidTest6 () { point point=NewPoint ( A, A); List<ChargePoi> venues =Mongotemplate.find (NewQuery (Criteria).where(" Location"). Nearsphere (Point). MaxDistance ( -)), Chargepoi.class); System. out. println (Venues.size ()); System. out. println (venues); }
7, nearest point query
@Test publicvoid test7 () { new point (12 ); = Nearquery.near (location). MaxDistance (new Distance (100000, metrics.kilometers)); Georesults<ChargePoi> result = mongotemplate.geonear (query, Chargepoi. Class); System. out . println (result); }
I am a hard-working porter:->http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.release/reference/html/mongo.core.html# Mongo.geospatial
Mongodb-mongotemplate Geo-coordinate operations