【问题标题】:Geospatial distance calculator using Sequelize & MySQL使用 Sequelize 和 MySQL 的地理空间距离计算器
【发布时间】:2017-11-24 07:55:21
【问题描述】:

我在 Sequelize 中有一个位置模型

var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
   var Location = sequelize.define('Location', {
      location_id: {
         type: Sequelize.STRING,
         primaryKey: true
      },
      location_code: {
         type: Sequelize.STRING,
         allowNull: true
      },
      location_name: {
         type: Sequelize.STRING,
         allowNull: true
      },
      latitude: {
         type: Sequelize.STRING,
         allowNull: true,
         defaultValue: null
      },
      longitude: {
         type: Sequelize.STRING,
         allowNull: true,
         defaultValue: null
      },
      location: {
         type: Sequelize.GEOMETRY('POINT'),
         allowNull: true
      }
   });
   return Location;
};

我通过喂食将纬度和经度插入位置

var location = { type: 'Point', coordinates: [latitude, longitude] };

我有一个以纬度和经度为参数的路由器。我需要找出提供的纬度/经度与位置表中所有位置之间的距离。

我该怎么做呢? Sequelize 是否已经为我提供了查找距离的功能,还是我需要编写外部查询才能找到它?

谢谢。

【问题讨论】:

标签: mysql sequelize.js geospatial latitude-longitude


【解决方案1】:

您不需要纬度和经度属性,它们将存储在您的位置属性中。 要查找距离小于 10000 米的地方,您可以使用以下方法:

    var lat = parseFloat(json.lat);
    var lng = parseFloat(json.lng);
    var attributes = Object.keys(Location.attributes);
    var location = sequelize.literal(`ST_GeomFromText('POINT(${lng} ${lat})')`);
    var distance = sequelize.fn('ST_Distance_Sphere', sequelize.literal('location'), location);
    attributes.push([distance,'distance']);

    Location.findAll({
      attributes: attributes,
      order: 'distance',
      where: sequelize.where(distance, {$lte: 10000}),
      logging: console.log
    })
    .then(function(instance){
      return res.json(200, instance);
    })

【讨论】:

  • 出错Argument of type '{ $lte: 10000; }' is not assignable to parameter of type 'LogicType'.@Edudjr
【解决方案2】:
 const { lat, lng, distance } = req.body.filter;

    Auction.findAndCountAll({
              include: [
                {
                  model: Property,
                  attributes: [
                    ['id', 'propertyID'],
                    ..............
                  ],
                  //lat lng and distance from angument
                  where: Auction.sequelize.and(
                    lat && lng && distance ? sequelize.where(
                      sequelize.literal(`6371 * acos(cos(radians(${lat})) * cos(radians(latitude)) * cos(radians(${lng}) - radians(longitude)) + sin(radians(${lat})) * sin(radians(latitude)))`),
                      '<=',
                      distance,
                    ) : null ,
                    {
                      status: 1,
                    }
                  ),

             //your code and logic
             .........
            }).then((result) => {
              ..........
            }).catch((err) => {
              ..........
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多