【问题标题】:How to find nearest coupon from cosmos db with current Latitude and Longitude如何使用当前纬度和经度从 cosmos db 中找到最近的优惠券
【发布时间】:2019-07-23 11:25:23
【问题描述】:

我在 Cosmos db 中有一个 Coupon 容器,其中包含 id、name、category、Latitude 和 Longitude 字段。我正在为不同的区域保存优惠券并保存这些区域的纬度和经度。我想搜索当前用户英里半径内的优惠券,例如 50 英里内。我从他们的设备获取用户位置的纬度和经度。经纬度格式为 lat: 24.8779 long: 67.0641。我正在使用 cosmos db 的 SQL API 来查询数据。请指导我对此的查询是什么?

【问题讨论】:

  • 我们需要知道 Lat & Long 字段的格式。请参阅此链接以获取快速参考:geomidpoint.com/latlon.html
  • 格式如24.8779

标签: c# sql-server azure azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

首先确保以 GeoJSON 格式存储优惠券坐标。例如,请参阅下面的位置字段:

{
  "country": "AD",
  "name": "Sant Julià de Lòria",
  "location": {
    "type": "Point",
    "coordinates": [
        42.46372,
        1.49129
    ]
  },
}

然后,使用ST_DISTANCE函数查询,例如:

SELECT * FROM c
WHERE ST_DISTANCE(c.location, {'type': 'Point', 'coordinates':[42.54277, 1.73361]}) < (50 * 1609.344)

ST_DISTANCE 使用米,所以乘以 1609.344 就是将英里转换为米。

请参阅https://docs.microsoft.com/en-us/azure/cosmos-db/geospatial 了解更多信息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多