均衡获取最近的位置获取点

时间:2019-07-30 23:54:23

标签: node.js sequelize.js

如何使用sequalize获取最近的位置,地理位置,

  var Address = sequelize.define('Address', {
    address: {type: DataTypes.STRING},
    house: {type: DataTypes.STRING},
    landmark: {type: DataTypes.STRING},
    type: {type: DataTypes.STRING},
    location: {type: DataTypes.GEOMETRY('POINT')},
  });

1 个答案:

答案 0 :(得分:0)

尝试这个:

const distance = sequelize.fn('ST_Distance',
    sequelize.cast(sequelize.fn('ST_SetSRID', sequelize.fn('ST_MakePoint', yourLongitude, yourLatitude), 4326), 'geography'),
    sequelize.cast(sequelize.fn('ST_SetSRID', 'location', 4326), 'geography'));

  Address.findOne({
    order: [[distance, 'ASC']],
    raw: true,
  });

因此,“距离”应该是一个数字,并显示您的位置与DB中某些对象之间的距离。从最小到最大排序并得到第一个。