Last active
June 5, 2023 10:26
-
-
Save elarcoiris/888d2b0c6a6bbcfe3676d8fd59b9ca0b to your computer and use it in GitHub Desktop.
Distance calculation - Sequelize model method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.getDistance = async (me) => { | |
try { | |
// Distance algorithm with Earth curvature | |
const users = await User.findAll({ | |
where: Sequelize.where(sequelize.literal(`6371 * acos(cos(radians(${me.latitude})) * cos(radians(latitude)) * cos(radians(${me.longitude}) - radians(longitude)) + sin(radians(${me.latitude})) * sin(radians(latitude)))`), | |
'<=', me.maxDistance), | |
attributes: ['id'] | |
}) | |
return users; | |
} | |
catch (error) { | |
console.log(error.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment