Using Sequelize and geospatial queries, if I want to find the \"n\" closest points to a certain location, how should the Sequelize query be?
Assume I have a model th
MySQL can give an error that function ST_Distance_Sphere
does not exist. In that case you can use this alternative solution:
I hold point information separately as latitude and longitude decimals. Assume you should have a model that looks something like this:
sequelize.define('Point', {latitude: DataTypes.DECIMAL(11,2)},
{longitude: DataTypes.DECIMAL(11,2)});
Imagine we have a lat
and lng
variables to define a location, and we want to find the 10 closest points to it:
db.Point.findAll({
attributes: [[sequelize.fn('POW',sequelize.fn('ABS',sequelize.literal("latitude-"+lat)),2),'x1'],
[sequelize.fn('POW',sequelize.fn('ABS',sequelize.literal("longitude-"+lng)),2),'x2']],
order: sequelize.fn('SQRT', sequelize.literal('x1+x2')),
limit: 10
});
Update:
With Haversine Formula, distance is more accurate:
db.Point.findAll({
attributes: [[sequelize.literal("6371 * acos(cos(radians("+lat+")) * cos(radians(latitude)) * cos(radians("+lng+") - radians(longitude)) + sin(radians("+lat+")) * sin(radians(latitude)))"),'distance']],
order: sequelize.col('distance'),
limit: 10
});