1

By sending latitude longitude of current Location, I would like to find the nearest airport in my collection.

I've used geodist for finding the distance between two latitute/longitude coordinates.

Question: Is it possible to make a query after getting all collection of airport to check whether the current location is available within a specific distance?

E.g. When minimum current distance to an item of the collections is within 100km, then I would like to show this item as result.

This My Query...

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

This My Route...

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

This my Controller...

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

2 回答 2

4

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

  /**
   * 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
  },
于 2019-04-10T13:04:17.830 回答
1

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

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;
于 2021-06-15T04:35:09.280 回答