SDK数据库 Geo


本文整理自网络,侵删。

Geo

数据库地理位置结构集


方法:

Geo.Point(longitude: number, latitude: number): GeoPoint

支持端:小程序 , 云函数 , Web

构造一个地理位置 ”点“。方法接受两个必填参数,第一个是经度(longitude),第二个是纬度(latitude),务必注意顺序。

参数

longitude: number

经度

latitude: number

纬度

返回值

GeoPoint

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: db.Geo.Point(113, 23)
  }
}).then(console.log).catch(console.error)

除了使用接口构造一个点外,也可以使用等价的 GeoJSON 的 点 (Point) 的 JSON 表示,其格式如下:

{
  "type": "Point",
  "coordinates": [longitude, latitude] // 数字数组:[经度, 纬度]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'Point',
      coordinates: [113, 23]
    }
  }
}).then(console.log).catch(console.error)


Geo.LineString(points: GeoPoint[]): GeoPoint

支持端:小程序 2.6.3, 云函数

构造一个地理位置的 ”线“。一个线由两个或更多的点有序连接组成。

参数

points: GeoPoint[]

“点” 数组

返回值

GeoPoint

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: db.Geo.LineString([
      db.Geo.Point(113, 23),
      db.Geo.Point(120, 50),
      // ... 可选更多点
    ])
  }
}).then(console.log).catch(console.error)

除了使用接口构造一条 LineString 外,也可以使用等价的 GeoJSON 的 线 (LineString) 的 JSON 表示,其格式如下:

{
  "type": "LineString",
  "coordinates": [
    [p1_lng, p1_lat],
    [p2_lng, p2_lng]
    // ... 可选更多点
  ]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'LineString',
      coordinates: [
        [113, 23],
        [120, 50]
      ]
    }
  }
}).then(console.log).catch(console.error)


Geo.Polygon(lineStrings: GeoLineString[]): GeoPolygon

支持端:小程序 2.6.3, 云函数

构造一个地理位置 ”多边形“

参数

lineStrings: GeoLineString[]

“线” 数组

返回值

GeoPolygon

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

说明

一个多边形由一个或多个线性环(Linear Ring)组成,一个线性环即一个闭合的线段。一个闭合线段至少由四个点组成,其中最后一个点和第一个点的坐标必须相同,以此表示环的起点和终点。如果一个多边形由多个线性环组成,则第一个线性环表示外环(外边界),接下来的所有线性环表示内环(即外环中的洞,不计在此多边形中的区域)。如果一个多边形只有一个线性环组成,则这个环就是外环。

多边形构造规则:

  1. 第一个线性环必须是外环
  2. 外环不能自交
  3. 所有内环必须完全在外环内
  4. 各个内环间不能相交或重叠,也不能有共同的边
  5. 外环应为逆时针,内环应为顺时针

示例代码

示例代码:单环多边形

const { Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: Polygon([
      LineString([
        Point(0, 0),
        Point(3, 2),
        Point(2, 3),
        Point(0, 0)
      ])
    ])
  }
}).then(console.log).catch(console.error)

示例代码:含一个外环和一个内环的多边形

const { Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: Polygon([
      // 外环
      LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
      // 内环
      LineString([ Point(10, 10), Point(16, 14), Point(14, 16), Point(10, 10) ])
    ])
  }
}).then(console.log).catch(console.error)

除了使用接口构造一个 Polygon 外,也可以使用等价的 GeoJSON 的 多边形 (Polygon) 的 JSON 表示,其格式如下:

{
  "type": "Polygon",
  "coordinates": [
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 外环
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可选内环 1
    ...
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可选内环 n
  ]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'Polygon',
      coordinates: [
        [ [0, 0], [30, 20], [20, 30], [0, 0] ],
        [ [10, 10], [16, 14], [14, 16], [10, 10]]
      ]
    }
  }
}).then(console.log).catch(console.error)


Geo.MultiPoint(points: GeoPoint[]): GeoMultiPoint

支持端:小程序 2.6.3, 云函数

构造一个地理位置的 ”点“ 的集合。一个点集合由一个或更多的点组成。

参数

points: GeoPoint[]

“点” 数组

返回值

GeoMultiPoint

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: db.Geo.MultiPoint([
      db.Geo.Point(113, 23),
      db.Geo.Point(120, 50),
      // ... 可选更多点
    ])
  }
}).then(console.log).catch(console.error)

除了使用接口构造 MultiPoint 外,也可以使用等价的 GeoJSON 的 点集合 (MultiPoint) 的 JSON 表示,其格式如下:

{
  "type": "MultiPoint",
  "coordinates": [
    [p1_lng, p1_lat],
    [p2_lng, p2_lng]
    // ... 可选更多点
  ]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'MultiPoint',
      coordinates: [
        [113, 23],
        [120, 50]
      ]
    }
  }
}).then(console.log).catch(console.error)


Geo.MultiPoint(points: GeoPoint[]): GeoMultiPoint

支持端:小程序 2.6.3, 云函数

构造一个地理位置的 ”点“ 的集合。一个点集合由一个或更多的点组成。

参数

points: GeoPoint[]

“点” 数组

返回值

GeoMultiPoint

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: db.Geo.MultiPoint([
      db.Geo.Point(113, 23),
      db.Geo.Point(120, 50),
      // ... 可选更多点
    ])
  }
}).then(console.log).catch(console.error)

除了使用接口构造 MultiPoint 外,也可以使用等价的 GeoJSON 的 点集合 (MultiPoint) 的 JSON 表示,其格式如下:

{
  "type": "MultiPoint",
  "coordinates": [
    [p1_lng, p1_lat],
    [p2_lng, p2_lng]
    // ... 可选更多点
  ]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'MultiPoint',
      coordinates: [
        [113, 23],
        [120, 50]
      ]
    }
  }
}).then(console.log).catch(console.error)


Geo.MultiLineString(lineStrings: GeoLineString[]): GeoMultiLineString

支持端:小程序 2.6.3, 云函数

构造一个地理位置 ”线“ 集合。一个线集合由多条线组成。

参数

lineStrings: GeoLineString[]

“线” 数组

返回值

GeoMultiLineString

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

示例代码

const { LineString, MultiLineString, Point } = db.Geo
db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: MultiLineString([
      LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
      LineString([ Point(10, 10), Point(16, 14), Point(14, 16), Point(10, 10) ])
    ])
  }
}).then(console.log).catch(console.error)

除了使用接口构造一个 MultiLineString 外,也可以使用等价的 GeoJSON 的 线集合 (MultiLineString) 的 JSON 表示,其格式如下:

{
  "type": "MultiLineString",
  "coordinates": [
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
    ...
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ]
  ]
}

示例代码

db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: {
      type: 'MultiLineString',
      coordinates: [
        [ [0, 0], [3, 3] ],
        [ [5, 10], [20, 30] ]
      ]
    }
  }
}).then(console.log).catch(console.error)


Geo.MultiPolygon(polygons: GeoPolygon[]): GeoMultiPolygon

支持端:小程序 2.6.3, 云函数

构造一个地理位置 ”多边形“ 集合。一个多边形集合由多个多边形组成。

参数

polygons: GeoPolygon[]

“多边形” 数组

返回值

GeoMultiPolygon

索引

如存储地理位置信息的字段有被查询的需求,务必对字段建立地理位置索引

说明

多边形集合由多个多边形组成。一个多边形由一个或多个线性环(Linear Ring)组成,一个线性环即一个闭合的线段。一个闭合线段至少由四个点组成,其中最后一个点和第一个点的坐标必须相同,以此表示环的起点和终点。如果一个多边形由多个线性环组成,则第一个线性环表示外环(外边界),接下来的所有线性环表示内环(即外环中的洞,不计在此多边形中的区域)。如果一个多边形只有一个线性环组成,则这个环就是外环。

阅读剩余部分

相关阅读 >>

微信小程序 getvisitpage

微信小程序api 音频-inneraudiocontext对象

微信小程序 调试

微信小程序 运力方使用updateorder

微信小程序api 实时音视频-liveplayercontext实例

微信小程序云开发 api数据库新增集合

微信小程序云开发 按量付费

微信小程序工具 程序调试的三大主要功能区

sdk数据库 command聚合操作符聚合表达式

微信小程序api 图片-压缩图片接口

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...