Dee*_*eek 4 spring-mvc geolocation mongodb
我想找到附近的位置所以插入这样的记录..
db.locationcol.insert({"location":"phase 8,mohali ,punjab ,india","service":"psychologist","loc":{"lon":76.703347,"lat":30.710459}})
Run Code Online (Sandbox Code Playgroud)
然后在终端上执行Query.
db.runCommand(
{
geoNear: "locationcol",
near: { type: "Point", coordinates: [ 76.720845, 30.712097 ] },
spherical: true,
query: { category: "public" }
})
Run Code Online (Sandbox Code Playgroud)
但它正在回归..
{ "ok" : 0, "errmsg" : "no geo indices for geoNear" }
Run Code Online (Sandbox Code Playgroud)
我也在尝试用Spring ...
public GeoResults getnearby(double longitude,double latitude, String service) {
Point point = new Point(longitude,latitude);
Query query = new Query(Criteria.where("service").is(service));
query.fields().include("service").include("location").include("loc");
NearQuery nearQuery = NearQuery.near(point).maxDistance(new Distance(50, Metrics.KILOMETERS));
nearQuery.query(query);
nearQuery.num(20);
GeoResults<locationcol> data = operations.geoNear(nearQuery, locationcol.class,"locationcol");
return data;
}
Run Code Online (Sandbox Code Playgroud)
这段代码返回空列表.我没有得到我出错的地方.救命 !!
Phi*_*ipp 14
在执行地理空间查询之前,您需要创建地理空间索引:
db.locationcol.createIndex( { loc : "2dsphere" } )
Run Code Online (Sandbox Code Playgroud)
此外,您需要将您的位置存储为有效的GeoJSON对象,以便MongoDB可以正确解析它们:
loc : { type: "Point", coordinates: [ -76.703347, 30.710459 ] },
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7295 次 |
| 最近记录: |