检索距离"dis". Near查询的结果 [英] Retrieving the Distance "dis" result from a Near query

查看:138
本文介绍了检索距离"dis". Near查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个地理空间查询,效果很好,但是我正在寻找每个结果的距离.

I've set up a geo-spacial query that works beautifully, however I'm looking to get the distance for each of the results.

给出查询:

var query = Query.Near("Location", longitude, latitude);
var places = mongoDb.GetCollection<Place>("places").Find(near);

如何检索位置"中每个元素的距离?

how do I retrieve the distance from each element in "places"?

推荐答案

我不知道您能否与mongo取得距离,但是我过去所做的只是使用Haversine公式来获得距离我有数据.

I don't know if you can get the distance from mongo, but what I have done in the past was just using the Haversine formula to get the distance after I had the data.

您可以找到我使用的实现 此处

You can find the implementation that I used here

编辑:抱歉,您可以从Mongo获得距离,但不能使用Query.Near().您需要使用集合中的GeoNear函数.

EDIT: Sorry you can get the distance back from Mongo, but not with Query.Near(). You need to use the GeoNear function that is on the collection.

 mongoDb.GetCollection<Place>("places").GeoNear(Query.Null, latitude, longitude, maxDistance);

这将返回一个GeoNearResult<Place>,它具有一个Hits属性,该属性为IEnumurable<Hit>,该属性将包含一个Distance属性,一个document属性将包含您的place对象.

This will return a GeoNearResult<Place> which will have a Hits property that is IEnumurable<Hit> that will contain a Distance property and a document property that will contain your place object.

这篇关于检索距离"dis". Near查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆