W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
數(shù)據(jù)庫地理位置結構集
支持端:小程序 , 云函數(shù) , Web
構造一個地理位置 ”點“。方法接受兩個必填參數(shù),第一個是經(jīng)度(longitude),第二個是緯度(latitude),務必注意順序。
經(jīng)度
緯度
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置的 ”線“。一個線由兩個或更多的點有序連接組成。
“點” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置 ”多邊形“
“線” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
一個多邊形由一個或多個線性環(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ī)則:
示例代碼:單環(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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。
“點” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。
“點” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置 ”線“ 集合。一個線集合由多條線組成。
“線” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
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)
支持端:小程序 2.6.3, 云函數(shù)
構造一個地理位置 ”多邊形“ 集合。一個多邊形集合由多個多邊形組成。
“多邊形” 數(shù)組
如存儲地理位置信息的字段有被查詢的需求,務必對字段建立地理位置索引
多邊形集合由多個多邊形組成。一個多邊形由一個或多個線性環(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ī)則:
示例代碼
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)
地理位置 “點”
經(jīng)度
緯度
返回相應的 GeoJSON 結構的對象
地理位置的 ”線“。一個線由兩個或更多的點有序連接組成。
“點” 數(shù)組
返回相應的 GeoJSON 結構的對象
地理位置 ”多邊形“
“線” 數(shù)組
返回相應的 GeoJSON 結構的對象
地理位置的 ”點“ 的集合。一個點集合由一個或更多的點組成。
“點” 數(shù)組
返回相應的 GeoJSON 結構的對象
地理位置 ”線“ 集合。一個線集合由多條線組成。
“線” 數(shù)組
返回相應的 GeoJSON 結構的對象
地理位置 ”多邊形“ 集合。一個多邊形集合由多個多邊形組成。
“多邊形” 數(shù)組
返回相應的 GeoJSON 結構的對象
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: