1

I want to implement a search filter where user can search by distance. I am storing the lat lng of user in DB.

  @property({
    type: 'string',
    default: null,
  })
  latitude?: string;

  @property({
    type: 'string',
    default: null,
  })
  longitude?: string;

I also tried adding these coordinates using POINT datatype but still no success.

So basically i want to get all nearby users by distance.

// current user coordinates const lat = Number(user.latitude); const long = Number(user.altitude);

if (!body.distance) {
  body.distance = 10;
}
const {distance} = body;
const a1 = lat - distance / 111.045;
const a2 = lat + distance / 111.045;
const b1 = long - distance / 111.045;
const b2 = long + distance / 111.045;

const results = await this.userRepository.find({
  where: {
    and: [{latitude: {near: [a1, a2]}}, {altitude: {near: [b1, b2]}}],
    id: {neq: id},
    category: body.category ? body.category : undefined,
    gender: body.gender.toLowerCase(),
  },
  fields: {
    hash: false,
    otp: false,
    forgotPasswordOtp: false,
    forgotPasswordTokenExpiresIn: false,
  },
  include: [
    {
      relation: 'places',
    },
  ],
  order: ['createdAt DESC'],
});

Someone here suggested to query like this by calculating the lat1,lat2,lng1,lng2 and filter all user who falls between these coordinates. But it doesn't seems to work coz i am getting empty array. I need a solution where i can find all user within the given distance e.g search filter like tinder.

I have also tried to store the lat , lng as point on Database but got some kind of error like invalid input syntax for type point = undefinded.

Please can someone help me implement this search filter.

Note- I am not trying to calculate distance from lat lng values. I just want to get data based on distance. If still have doubts please let me known in the comments

callmeizaz
  • 249
  • 3
  • 9

0 Answers0