如何在 Firebase Cloud Firestore 中保存 GeoPoint?

How to save GeoPoint in Firebase Cloud Firestore?

在数据库 UI 中,我可以创建类型为 geopoint 的字段。提供值后,它看起来像这样:

location: [1° N, 1° E]

我正在尝试通过客户端 SDK(网络)将其保存到数据库中。 根据 Twitter 反馈,我尝试了 GeoJSON、Coords Array 和 Coords Object:

location: [1, 2];
location: {
    latitude: 1,
    longitude: 2,
};
location: {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [125.6, 10.1]
    }
}

None 其中生成了数据库中的地理点类型。它们只是保存为 Objects/Arrays.

如何通过 Web SDK 将 GeoPoint 保存在 Firebase Cloud Firestore 中?

Firestore SDK 包含 GeoPoint class,因此您必须像这样保存位置:

{
  location: new firebase.firestore.GeoPoint(latitude, longitude)
}

只需使用:

import 'package:cloud_firestore/cloud_firestore.dart';
{
  location:GeoPoint(latitude, longitude)
}

我努力寻找合适的import/require。这对我有用!!

const firebaseAdmin = require('firebase-admin');
//other code ....

//assigning value to myLocation attribute
let newObject = {
    otherAttribute:"otherAttributeValue",
    myLocation: new firebaseAdmin.firestore.GeoPoint(latitude,longitude)
}

PHP 使用“google/cloud-firestore”包

你必须使用这个classGoogle\Cloud\Core\GeoPoint

示例:

<?php

$geoPoint = new \Google\Cloud\Core\GeoPoint($latitude , $longitude);
$data = ['geopoint' => $geoPoint];
 GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
    _firestore
        .collection('locations')
        .add({'name': 'random name', 'position': geoFirePoint.data}).then((_) {
      print('added ${geoFirePoint.hash} successfully');
    });

版本 9 Firestore

import { GeoPoint} from "firebase/firestore";
const dataDocument = {
 tienda: new GeoPoint(latitude: number, longitude: number),
}