SDK數(shù)據(jù)庫 Geo

2022-05-12 16:51 更新

Geo

數(shù)據(jù)庫地理位置結構集


方法:

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

支持端:小程序 , 云函數(shù) , Web

構造一個地理位置 ”點“。方法接受兩個必填參數(shù),第一個是經(jīng)度(longitude),第二個是緯度(latitude),務必注意順序。

參數(shù)

longitude: number

經(jīng)度

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] // 數(shù)字數(shù)組:[經(jīng)度, 緯度]
}

示例代碼

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, 云函數(shù)

構造一個地理位置的 ”線“。一個線由兩個或更多的點有序連接組成。

參數(shù)

points: GeoPoint[]

“點” 數(shù)組

返回值

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, 云函數(shù)

構造一個地理位置 ”多邊形“

參數(shù)

lineStrings: GeoLineString[]

“線” 數(shù)組

返回值

GeoPolygon

索引

如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引

說明

一個多邊形由一個或多個線性環(huán)(Linear Ring)組成,一個線性環(huán)即一個閉合的線段。一個閉合線段至少由四個點組成,其中最后一個點和第一個點的坐標必須相同,以此表示環(huán)的起點和終點。如果一個多邊形由多個線性環(huán)組成,則第一個線性環(huán)表示外環(huán)(外邊界),接下來的所有線性環(huán)表示內(nèi)環(huán)(即外環(huán)中的洞,不計在此多邊形中的區(qū)域)。如果一個多邊形只有一個線性環(huán)組成,則這個環(huán)就是外環(huán)。

多邊形構造規(guī)則:

  1. 第一個線性環(huán)必須是外環(huán)
  2. 外環(huán)不能自交
  3. 所有內(nèi)環(huán)必須完全在外環(huán)內(nèi)
  4. 各個內(nèi)環(huán)間不能相交或重疊,也不能有共同的邊
  5. 外環(huán)應為逆時針,內(nèi)環(huán)應為順時針

示例代碼

示例代碼:單環(huán)多邊形

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)

示例代碼:含一個外環(huán)和一個內(nèi)環(huán)的多邊形

const { Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: Polygon([
      // 外環(huán)
      LineString([ Point(0, 0), Point(30, 20), Point(20, 30), Point(0, 0) ]),
      // 內(nèi)環(huán)
      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] ], // 外環(huán)
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可選內(nèi)環(huán) 1
    ...
    [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ], // 可選內(nèi)環(huán) 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, 云函數(shù)

構造一個地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。

參數(shù)

points: GeoPoint[]

“點” 數(shù)組

返回值

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, 云函數(shù)

構造一個地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。

參數(shù)

points: GeoPoint[]

“點” 數(shù)組

返回值

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, 云函數(shù)

構造一個地理位置 ”線“ 集合。一個線集合由多條線組成。

參數(shù)

lineStrings: GeoLineString[]

“線” 數(shù)組

返回值

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, 云函數(shù)

構造一個地理位置 ”多邊形“ 集合。一個多邊形集合由多個多邊形組成。

參數(shù)

polygons: GeoPolygon[]

“多邊形” 數(shù)組

返回值

GeoMultiPolygon

索引

如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引

說明

多邊形集合由多個多邊形組成。一個多邊形由一個或多個線性環(huán)(Linear Ring)組成,一個線性環(huán)即一個閉合的線段。一個閉合線段至少由四個點組成,其中最后一個點和第一個點的坐標必須相同,以此表示環(huán)的起點和終點。如果一個多邊形由多個線性環(huán)組成,則第一個線性環(huán)表示外環(huán)(外邊界),接下來的所有線性環(huán)表示內(nèi)環(huán)(即外環(huán)中的洞,不計在此多邊形中的區(qū)域)。如果一個多邊形只有一個線性環(huán)組成,則這個環(huán)就是外環(huán)。

多邊形構造規(guī)則:

  1. 第一個線性環(huán)必須是外環(huán)
  2. 外環(huán)不能自交
  3. 所有內(nèi)環(huán)必須完全在外環(huán)內(nèi)
  4. 各個內(nèi)環(huán)間不能相交或重疊,也不能有共同的邊
  5. 外環(huán)應為逆時針,內(nèi)環(huán)應為順時針

示例代碼

示例代碼

const { MultiPolygon, Polygon, LineString, Point } = db.Geo
db.collection('todos').add({
  data: {
    description: 'eat an apple',
    location: MultiPolygon([
      Polygon([
        LineString([ Point(50, 50), Point(60, 80), Point(80, 60), Point(50, 50) ]),
      ]),
      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)

除了使用接口構造一個 MultiPolygon 外,也可以使用等價的 GeoJSON 的 多邊形 (MultiPolygon) 的 JSON 表示,其格式如下:

{
  "type": "MultiPolygon",
  "coordinates": [
    // polygon 1
    [
      [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
      [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ],
      ...
      [ [lng, lat], [lng, lat], [lng, lat], ..., [lng, lat] ]
    ],
    ...
    // polygon n
    [
      [ [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: 'MultiPolygon',
      coordinates: [
        [
          [ [50, 50], [60, 80], [80, 60], [50, 50] ]
        ],
        [
          [ [0, 0], [30, 20], [20, 30], [0, 0] ],
          [ [10, 10], [16, 14], [14, 16], [10, 10]]
        ]
      ]
    }
  }
}).then(console.log).catch(console.error)



類型:

GeoPoint

地理位置 “點”

屬性

longitude: number

經(jīng)度

latitude: number

緯度

方法

GeoPoint.toJSON(): Object

返回相應的 GeoJSON 結構的對象


GeoLineString

地理位置的 ”線“。一個線由兩個或更多的點有序連接組成。

屬性

points: GeoPoint[]

“點” 數(shù)組

方法

GeoLineString.toJSON(): Object

返回相應的 GeoJSON 結構的對象


GeoPolygon

地理位置 ”多邊形“

屬性

lines: GeoLineString[]

“線” 數(shù)組

方法

GeoPolygon.toJSON(): Object

返回相應的 GeoJSON 結構的對象


GeoMultiPoint

地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。

屬性

points: GeoPoint[]

“點” 數(shù)組

方法

GeoMultiPoint.toJSON(): Object

返回相應的 GeoJSON 結構的對象


GeoMultiLineString

地理位置 ”線“ 集合。一個線集合由多條線組成。

屬性

lines: GeoLineString[]

“線” 數(shù)組

方法

GeoMultiLineString.toJSON(): Object

返回相應的 GeoJSON 結構的對象


GeoMultiPolygon

地理位置 ”多邊形“ 集合。一個多邊形集合由多個多邊形組成。

屬性

polygons: GeoPolygon[]

“多邊形” 數(shù)組

方法

GeoMultiPolygon.toJSON(): Object

返回相應的 GeoJSON 結構的對象




以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號