MongoDB $ geoNear distance与Google Maps distance不同 [英] MongoDB $geoNear distance is different than Google Maps distance

查看:163
本文介绍了MongoDB $ geoNear distance与Google Maps distance不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中有一个场地列表,我希望按距离客户的顺序显示这些列表。



我对数据库的查询如下:

  venueCollection.aggregate([
{
'$ geoNear':{
'near ':{
'type':'Point',
'coordinates':[user_lng,user_lat]
},
'spherical':true,
'distanceField ':'dist'
}
},
{
'$ sort':{
'dist':1
}
}
],函数(err,场地){
//我的代码...
});

一个简单的文件:

  {
_id:ObjectId(56ec1c833e017f403870e2c5),
name:A& H,
city:'s- Gravenhage,
province:Zuid Holland,
location:{
type:Point,
coordinates:[
4.276930300000004,
52.0732475
]
}
}

它显示了一个Venues列表,距离字段是正确的,从最近到最远。



客户端I使用纬度经度(与我传递给服务器和场地相同)来计算与Google的距离地图:

  var venuePosition = new google.maps.LatLng(attrs.lat,attrs.lng); 
var distance = google.maps.geometry.spherical.computeDistanceBetween(scope.user.position.compiled,venuePosition);
element.text((distance / 1000).toFixed(2)+'km');

正确显示2点之间的距离。

问题是这两个值(来自Mongo和gMaps)是不同的,即使列表使用mongoDB中的 $ sort 正确显示,它显示的距离是不在订单中,即:

 场地01:21.350km 
场地02:21.412km
场地03:21.388km //错误!!!
场地04:21.547km

错误在哪里?
我需要按照距离顺序对场地进行排序,并以km为单位显示距离......并且它应该被排序......

解决方案

MongoDB假定地球是完美的球体,并使用平均地球半径来计算两点之间的距离。但是,如你所知,地球不是球体,而是更多的椭球体,所以距离计算并不是完美的,但在MongoDB中是近似的。但是,Google使用非常复杂的公式来获得更好的结果。他们在地球上的不同位置确实拥有巨大的地球半径数据库。



请参阅本网站,其中介绍并使用在线计算器来计算两点之间的距离,使用平均地球半径。



http://andrew.hedges。 name / experiments / haversine /



您会看到由上面的网站和MongoDB计算出的距离是相同的。

但是,如果您想要获得接近准确的结果,请参阅以下链接 http ://www.movable-type.co.uk/scripts/latlong.html 。它使用非常复杂的算法来获得更好的结果。

I have a list of Venues in my DB and I wish to show those in order of distance to the client.

My query to the DB is the following:

    venueCollection.aggregate([
        {
            '$geoNear': {
                'near': {
                    'type': 'Point',
                    'coordinates': [ user_lng , user_lat ]
                },
                'spherical': true, 
                'distanceField': 'dist'
            }
        },
        {
            '$sort': {
                'dist': 1
            }
        }
    ], function (err, venues) {
        // my code...
    });

A simple Document:

{
    "_id" : ObjectId("56ec1c833e017f403870e2c5"),
    "name" : "A & H",
    "city" : "'s-Gravenhage",
    "province" : "Zuid Holland",
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            4.276930300000004, 
            52.0732475
        ]
    }
}

It show a list of Venues and the distance field is correct, from closest to farthest.

Client side I use the latitude and longitude (same I pass to the server and the same of the venue) to calculate distance with Google Maps:

      var venuePosition = new google.maps.LatLng(attrs.lat, attrs.lng);
      var distance = google.maps.geometry.spherical.computeDistanceBetween(scope.user.position.compiled, venuePosition);
      element.text((distance / 1000).toFixed(2) + ' km');

it show correctly the distance between 2 points.

The issue is that the 2 values (from Mongo and from gMaps) are different and even if the list is correctly displayed using $sort from mongoDB it show distances that are not in Order, ie.:

venue 01: 21.350km
venue 02: 21.412km
venue 03: 21.388km // wrong!!!
venue 04: 21.547km

Where is the error? I need to sort my venues in distance order and show the distance in km... and it should be ordered too...

解决方案

MongoDB assumes earth is perfect sphere and uses average radius of earth to calculate distance between two points. But as you know earth is not sphere but more sort of Ellipsoid so distance calculation will not be perfect but approximate in MongoDB. However, Google uses very complex formula to get better results. They do have huge database of radius of earth at different locations on earth.

See this website which explains and have online calculator to compute distance between two points using average radius of earth.

http://andrew.hedges.name/experiments/haversine/

You will see that distance calculated by above website and MongoDB are same.

However, if you want to get near accurate result, see following link http://www.movable-type.co.uk/scripts/latlong.html. It uses very complex algorithem to get better result.

这篇关于MongoDB $ geoNear distance与Google Maps distance不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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