uni-app中为什么geoNear里的maxDistance无效?

uni-app中为什么geoNear里的maxDistance无效?

var {community, location}=that,  
{dynamic}=community,  
{longitude, latitude}=location,  
maxDistance=data.maxDistance||50000,  
geoNear = {  
    distanceField: 'distance',  
    spherical: true,  
    near: new db.Geo.Point(longitude, latitude),  
    query: {  
        offShelves:_.neq(true)  
    },  
    maxDistance  
};  
console.log(maxDistance)  
dynamic.status='loading';  
db.collection('xn-user-action').aggregate().geoNear(geoNear).lookup({  
    from: 'xn-community',  
    let: {  
        id: '$communityID',  
    },  
    pipeline: $.pipeline().match(_.expr($.eq(['$_id','$$id']))).project({public:1, name:1}).done(),  
    as: 'community'  
}).lookup({  
    from: 'uni-id-users',  
    let: {  
        creator: '$creatorID',  
    },  
    pipeline: $.pipeline().match(_.expr($.eq(['$_id','$$creator']))).project({avatar_file:1,nickname:1}).done(),  
    as: 'creatorID'  
}).match({  
    "community.0.public":_.neq(false)  
}).limit(20).end().then(res=>{  
    let list=res.result.data;  
    if(list.length<20){  
        dynamic.status='nomore';  
    }else{  
        dynamic.status='loadmore';  
    };  
    resolve(list);  
}).catch(err=>{  
    reject(err);  
})

更多关于uni-app中为什么geoNear里的maxDistance无效?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

此问题已在3.3.1版本修复

更多关于uni-app中为什么geoNear里的maxDistance无效?的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这段代码写在客户端还是云函数的?传入的maxDistance是多少?列表内的距离是怎么计算出来的?

我也遇到这个问题,在云函数里面写的聚合查询,限制30公里以内,结果是1000公里以外的都查询出来了 async search(data, uid) {
const cmd = this.db.command;
let {
longitude,
latitude,
type,
} = data;

    let result = await this.collection.aggregate()  
        .geoNear({  
            distanceField: 'juli', // 输出的每个记录中 distance 即是与给定点的距离  
            spherical: true,  
            near: new this.db.Geo.Point(longitude, latitude),  
            maxDistance: 30000, //限制30km  
            minDistance: 0,  
            query: {  
                isOnline: true,  
                type: type,  
            },  
            key: 'home_address.location', // 若只有 location 一个地理位置索引的字段,则不需填  
            includeLocs: 'home_address.location', // 若只有 location 一个是地理位置,则不需填  
        })  
        .project({  
            mobile: 0  
        })  
        .skip((data.page - 1) * data.limit)  
        .limit(data.limit)  
        .end()  
    return result.data;  
}<br>

腾讯那边这个参数的处理确实有问题,我们联系腾讯修复下

希望修复好后通知一下,,,

不过我是运行在客户端的,非云函数端,

回复 1***@qq.com: 云端和客户端是一样的

诶~也遇到这个问题,看到这里说明之前被浪费了大把时间。

在uni-app云函数中使用geoNear时,maxDistance参数无效通常是由于单位设置问题导致的。根据MongoDB官方文档,geoNearmaxDistance参数默认单位是弧度,而不是米或公里。

从你的代码看,你设置的maxDistance值为50000(默认值),这相当于50000弧度,显然不符合实际距离筛选需求。要按米为单位进行距离筛选,需要进行单位转换:

解决方案:maxDistance值除以地球半径(约6378100米)转换为弧度:

maxDistance: (data.maxDistance || 50000) / 6378100

或者更精确的写法:

maxDistance: (data.maxDistance || 50000) / 6371000  // 使用平均地球半径6371公里

完整修正后的geoNear配置:

geoNear = {  
    distanceField: 'distance',  
    spherical: true,  
    near: new db.Geo.Point(longitude, latitude),  
    query: {  
        offShelves: _.neq(true)  
    },  
    maxDistance: (data.maxDistance || 50000) / 6378100
};
回到顶部