Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SDK database Geo


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Geo

The set of database geo-location structures


Method:

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

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.

Parameters

longitude: number

Longitude

latitude: number

Latitude

Returns a value

GeoPoint

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

The sample code

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)


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

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.

Parameters

points: GeoPoint[]

The "dot" array

Returns a value

GeoPoint

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

The sample code

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)


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

Support side: Small program 2.6.3, cloud function

Construct a geographic location "polygon"

Parameters

lineStrings: GeoLineString[]

Line array

Returns a value

GeoPolygon

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

Description

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:

  1. The first linear ring must be an outer ring
  2. The outer ring cannot be self-confessed
  3. All inner rings must be completely inside the outer ring
  4. The inner rings cannot intersect or overlap, nor can they have common edges
  5. The outer ring should be counterclockwise and the inner ring should be clockwise

The sample code

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)


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

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.

Parameters

points: GeoPoint[]

The "dot" array

Returns a value

GeoMultiPoint

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

The sample code

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)


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

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.

Parameters

points: GeoPoint[]

The "dot" array

Returns a value

GeoMultiPoint

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

The sample code

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)


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

Support side: Small program 2.6.3, cloud function

Construct a geo-location "line" collection. A collection of lines consists of multiple lines.

Parameters

lineStrings: GeoLineString[]

Line array

Returns a value

GeoMultiLineString

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

The sample code

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)


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

Support side: Small program 2.6.3, cloud function

Construct a geo-location "polygon" collection. A collection of polygons consists of multiple polygons.

Parameters

polygons: GeoPolygon[]

Polygon array

Returns a value

GeoMultiPolygon

Index

If a field that stores geographic information has a need to be queried, be sure to index the field for geographic location

Description

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:

  1. The first linear ring must be an outer ring
  2. The outer ring cannot be self-confessed
  3. All inner rings must be completely inside the outer ring
  4. The inner rings cannot intersect or overlap, nor can they have common edges
  5. The outer ring should be counterclockwise and the inner ring should be clockwise

The sample code

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)



Type:

GeoPoint

Location "point"

Property

longitude: number

Longitude

latitude: number

Latitude

Method

GeoPoint.toJSON(): Object

Returns the object for the appropriate GeoJSON structure


GeoLineString

The "line" of the geographic location. A line consists of two or more points connected in an orderly manner.

Property

points: GeoPoint[]

The "dot" array

Method

GeoLineString.toJSON(): Object

Returns the object for the appropriate GeoJSON structure


GeoPolygon

Location "Polygon"

Property

lines: GeoLineString[]

Line array

Method

GeoPolygon.toJSON(): Object

Returns the object for the appropriate GeoJSON structure


GeoMultiPoint

A collection of "points" for a geographic location. A collection of points consists of one or more points.

Property

points: GeoPoint[]

The "dot" array

Method

GeoMultiPoint.toJSON(): Object

Returns the object for the appropriate GeoJSON structure


GeoMultiLineString

Geolocation "line" collection. A collection of lines consists of multiple lines.

Property

lines: GeoLineString[]

Line array

Method

GeoMultiLineString.toJSON(): Object

Returns the object for the appropriate GeoJSON structure


GeoMultiPolygon

Geolocation Polygon collection. A collection of polygons consists of multiple polygons.

Property

polygons: GeoPolygon[]

Polygon array

Method

GeoMultiPolygon.toJSON(): Object

Returns the object for the appropriate GeoJSON structure