【问题标题】:Sequalize get nearest location get pointSequelize 获取最近的位置获取点
【发布时间】:2019-08-02 00:18:33
【问题描述】:

如何使用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')},
  });

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    试试这个:

    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 中某个对象之间的距离。从最小到最大排序并获得第一个。

    【讨论】:

    • 我收到此错误。 UnhandledPromiseRejectionWarning: SequelizeDatabaseError: You have an error in your SQL syntax;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“GEOGRAPHY), CAST(ST_SetSRID('location', 4326) AS GEOGRAPHY)) ASC LIMIT 1' 附近使用正确的语法
    猜你喜欢
    • 2012-11-15
    • 2020-11-06
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2011-04-11
    相关资源
    最近更新 更多