【问题标题】:Mongo $geoWithin RadiansMongo $geoWithin 弧度
【发布时间】:2014-09-19 03:24:12
【问题描述】:

努力计算圆的弧度使用 mongoose/mongodb geoWithin 查询。我有用户的位置,我的结果离那个用户最远。从这两点开始,我需要计算弧度,以便我可以围绕该用户进行一整圈搜索,以获得最远的结果。希望这是有道理的。 :)

    //get closest item to user (just because)
    var closestLon = vendorPosts[0].location.long;
    var closestLat = vendorPosts[0].location.lat;

    //get furthest item from user
    var furthestLon = vendorPosts[vendorPosts.length - 1].location.long;
    var furthestLat = vendorPosts[vendorPosts.length - 1].location.lat;

    //User is located here
    var userLat = req.query.lat;
    var userLon = req.query.long;

    //Number of kilometers furthest item is from user
    var maxDistanceKm = getDistanceFromLatLonInKm(userLat, userLon, furthestLat, furthestLon);

    //Attempting to convert to rads but results never appear, is this correct calculation?
    var maxDistanceRadians = maxDistanceKm/6371;

    Model.find({
        $text: { $search: searchterm, $language: 'english' },
        coordinates :
             { $geoWithin :
                 {
                     $center : [ [ userLon, userLat ] , maxDistanceRadians ]
                 }
             }
        } , { score: {
                $meta: "textScore"
            }
        }, function (err, results) {
            ....
        });

使用这些著名的半正弦公式/工具

    function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
        var R = 6371; // Radius of the earth in miles
        var dLat = deg2rad(lat2-lat1);  // deg2rad below
        var dLon = deg2rad(lon2-lon1);
        var a =
              Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
              Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c; // Distance in km
        return d;
    }

    function deg2rad(deg) {
        return deg * (Math.PI/180)
    }

【问题讨论】:

标签: mongodb geospatial


【解决方案1】:

看起来我的回答过于复杂了。我已经有了一个圆的中心点(用户位置),并且我已经有了离用户最远的项目。从这两点我需要计算半径并插入 mongo。

    var furthestitem = vendorPosts[vendorPosts.length - 1];

    var radius = Math.sqrt( Math.pow((userLat-furthestitem.location.lat), 2) + Math.pow((userLong-furthestitem.location.long), 2) )

    PostModel.find({
        $text: { $search: heading, $language: 'english' },
        $or: mongoCategories,
        coordinates :
             { $geoWithin :
                 {
                     $center : [ [ userLong, userLat ] , radius ]
                 }
             }
        } , { score: {
                $meta: "textScore"
            }
        }, function (err, results) {  ...... });

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 2018-08-24
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多