Strapi API:设置自定义查询服务以查找范围内最近的位置?



通过发送当前位置的纬度经度,我想在我的收藏中找到最近的机场。

我使用大地地理学家来查找两个纬度/经度坐标之间的距离。

问:是否可以在获得所有机场收集后进行查询,以检查当前位置在特定距离内是否可用?

例如,当到收藏品的最小当前距离在 100 公里以内时,我想显示该项目作为结果。

这是我的查询...

return Airport
.find(_.pick(params, _.keys(Airport.schema.paths)))
.populate(_.keys(_.groupBy(_.reject(
strapi.models.airport.associations, {autoPopulate: false}), 'alias')).join(' '));

这是我的路线...

{
"method": "GET",
"path": "/airport/:latitude/:longitude/",
"handler": "Airport.findOne",
"config": {
"policies": []
}
},

这是我的控制器...

findOne: async (ctx) => {
return strapi.services.airport.fetch(ctx.params);
},

也许你已经解决了,但如果可以帮助某人,我的方法是:

/**
* Promise to fetch all near shops.
*
* @return {Promise}
*/
fetchAllNear: async (params) => {
const convertedParams = strapi.utils.models.convertParams('shops', params)
let data = {}
let populatedShopsActive = {}
data = await new Promise((resolve, reject) => {
Shops.aggregate([
{
'$geoNear': {
'near': { type: 'Point', coordinates: [ parseFloat(params._lon), parseFloat(params._lat) ] },
'limit': 100,
'maxDistance': parseFloat(params._distance),
'distanceField': 'distance',
'distanceMultiplier': 0.001,
'spherical': true
}
}
]).exec(async (err, shops) => {
await Shops.populate(shops, {
path: _.keys(_.groupBy(_.reject(strapi.models.shops.associations, {autoPopulate: false}), 'alias')).join(' '),
populate: {
path: 'shop image',
populate: {
path: 'image'
}
}
}, async (err, populatedShops) => {
if (err) {
reject(err)
} else {
if (params._todayOffers === 'true') {
populatedShopsActive = await _.forEach(populatedShops, async (shop) => {
await _.remove(shop.offers, (offer) => {
return !(_moment(offer.end_date).isSame(new Date(), 'day'))
})
})
} else {
populatedShopsActive = await _.forEach(populatedShops, async (shop) => {
await _.remove(shop.offers, (offer) => {
return !(_moment(new Date()).isBetween(offer.start_date, offer.end_date, null, '[]'))
})
})
}
resolve(populatedShopsActive)
}
})
})
})
return data
},

我得到了我的情况的解决方案:

let entities;
entities = await strapi.services.projects.find({
geo_location: {
$near: {
$geometry: {
type: "Point",
coordinates: [21.1702401, 72.83106070000001],
},
$minDistance: 0,
$maxDistance: 5000,
},
},
});
return entities;

最新更新