Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Mongo$Geo弧度以内_Mongodb_Geospatial - Fatal编程技术网

Mongodb Mongo$Geo弧度以内

Mongodb Mongo$Geo弧度以内,mongodb,geospatial,Mongodb,Geospatial,使用mongoose/mongodb GeoInQuery计算圆的弧度。我有用户的位置,我知道离该用户最远的结果。从这两点开始,我需要计算弧度,这样我就可以围绕用户搜索最远的结果。希望这是有道理的 //get closest item to user (just because) var closestLon = vendorPosts[0].location.long; var closestLat = vendorPosts[0].location.lat;

使用mongoose/mongodb GeoInQuery计算圆的弧度。我有用户的位置,我知道离该用户最远的结果。从这两点开始,我需要计算弧度,这样我就可以围绕用户搜索最远的结果。希望这是有道理的

    //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) {
            ....
        });
使用这些著名的哈弗森公式/UTIL

    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)
    }

看起来我的答案太复杂了。我已经有了圆形用户位置的中心点,并且我已经有了离用户最远的项目。从这两点开始,我需要计算半径并插入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) {  ...... });

公式看起来正确,您可以在此处看到要检查的实现。你到底有什么问题?