May 20, 2021 WeChat Mini Program Development Document
The set of database geo-location structures
Support: small programs, cloud functions, web
Construct a geographic location "point." Method accepts two required parameters, the first is longitude and the second is latitude, so it is important to pay attention to the order.
Longitude
Latitude
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
db.collection('todos').add({
data: {
description: 'eat an apple',
location: db.Geo.Point(113, 23)
}
}).then(console.log).catch(console.error)
In addition to constructing a point using an interface, you can also use the JSON of the equivalent GeoJSON's Point, in the following format:
{
"type": "Point",
"coordinates": [longitude, latitude] // 数字数组:[经度, 纬度]
}
The sample code
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'Point',
coordinates: [113, 23]
}
}
}).then(console.log).catch(console.error)
Support side: Small program 2.6.3, cloud function
Construct a geographic "line." A line consists of two or more points connected in an orderly manner.
The "dot" array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
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)
In addition to constructing a LineString using an interface, you can also use the JSON of the equivalent GeoJSON line, in the following format:
{
"type": "LineString",
"coordinates": [
[p1_lng, p1_lat],
[p2_lng, p2_lng]
// ... 可选更多点
]
}
The sample code
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'LineString',
coordinates: [
[113, 23],
[120, 50]
]
}
}
}).then(console.log).catch(console.error)
Support side: Small program 2.6.3, cloud function
Construct a geographic location "polygon"
Line array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
A polygon consists of one or more linear rings, a linear ring, or a closed segment. A closed segment consists of at least four points, where the coordinates of the last point and the first point must be the same to represent the start and end points of the ring. I f a polygon consists of more than one linear ring, the first linear ring represents the outer ring (outer boundary), and all subsequent linear rings represent the inner ring (that is, the hole in the outer ring, not counting the area in the polygon). If a polygon consists of only one linear ring, the ring is the outer ring.
Polygon construction rules:
Example code: Single-loop polygon
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)
Example code: Polygon with an outer ring and an inner ring
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)
In addition to constructing a Polygon using an interface, you can also use the JSON of the equivalent GeoJSON Polygon in the following format:
{
"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
]
}
The sample code
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)
Support side: Small program 2.6.3, cloud function
Constructs a collection of "points" for a geographic location. A collection of points consists of one or more points.
The "dot" array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
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)
In addition to constructing MultiPoint using interfaces, you can also use the JSON of the equivalent GeoJSON point collection (MultiPoint) in the following format:
{
"type": "MultiPoint",
"coordinates": [
[p1_lng, p1_lat],
[p2_lng, p2_lng]
// ... 可选更多点
]
}
The sample code
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'MultiPoint',
coordinates: [
[113, 23],
[120, 50]
]
}
}
}).then(console.log).catch(console.error)
Support side: Small program 2.6.3, cloud function
Constructs a collection of "points" for a geographic location. A collection of points consists of one or more points.
The "dot" array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
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)
In addition to constructing MultiPoint using interfaces, you can also use the JSON of the equivalent GeoJSON point collection (MultiPoint) in the following format:
{
"type": "MultiPoint",
"coordinates": [
[p1_lng, p1_lat],
[p2_lng, p2_lng]
// ... 可选更多点
]
}
The sample code
db.collection('todos').add({
data: {
description: 'eat an apple',
location: {
type: 'MultiPoint',
coordinates: [
[113, 23],
[120, 50]
]
}
}
}).then(console.log).catch(console.error)
Support side: Small program 2.6.3, cloud function
Construct a geo-location "line" collection. A collection of lines consists of multiple lines.
Line array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
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)
In addition to constructing a MultiLineString using an interface, you can also use the JSON of the equivalent GeoJSON's line collection (MultiLineString) in the following format:
{
"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] ]
]
}
The sample code
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)
Support side: Small program 2.6.3, cloud function
Construct a geo-location "polygon" collection. A collection of polygons consists of multiple polygons.
Polygon array
If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location
A polygon collection consists of multiple polygons. A polygon consists of one or more linear rings, a linear ring, or a closed segment. A closed segment consists of at least four points, where the coordinates of the last point and the first point must be the same to represent the start and end points of the ring. I f a polygon consists of more than one linear ring, the first linear ring represents the outer ring (outer boundary), and all subsequent linear rings represent the inner ring (that is, the hole in the outer ring, not counting the area in the polygon). If a polygon consists of only one linear ring, the ring is the outer ring.
Polygon construction rules:
The sample code
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)
In addition to constructing a MultiPolygon using an interface, you can also use the JSON of the equivalent GeoJSON Polygon, in the following format:
{
"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] ]
],
]
}
The sample code
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)
Location "point"
Longitude
Latitude
Returns the object for the appropriate GeoJSON structure
The "line" of the geographic location. A line consists of two or more points connected in an orderly manner.
The "dot" array
Returns the object for the appropriate GeoJSON structure
Location "Polygon"
Line array
Returns the object for the appropriate GeoJSON structure
A collection of "points" for a geographic location. A collection of points consists of one or more points.
The "dot" array
Returns the object for the appropriate GeoJSON structure
Geolocation "line" collection. A collection of lines consists of multiple lines.
Line array
Returns the object for the appropriate GeoJSON structure
Geolocation Polygon collection. A collection of polygons consists of multiple polygons.
Polygon array
Returns the object for the appropriate GeoJSON structure