【问题标题】:How to save GeoPoint in Firebase Cloud Firestore?如何在 Firebase Cloud Firestore 中保存 GeoPoint?
【发布时间】:2017-10-06 10:29:07
【问题描述】:

在数据库 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]
    }
}

这些都没有导致数据库中的地理点类型。它们只是保存为对象/数组。

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

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

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

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

    【讨论】:

    • 你导入了什么来获取 GeoPoint 类?我尝试了 firebase 和 firebase/app,但没有成功。我在一个 firebase-admin 应用中。
    • @neonguru - 你更新到最新的管理 SDK 了吗?它应该在那里firebase.google.com/docs/reference/admin/node/…
    • @Scarygami - 谢谢,这就解释了。我有最新的,但我需要使用 firebase-admin 的导入变量。再次感谢! WebStorm 自动从 firebase/app 使用它。
    • @neonguru 你能举个例子吗?我也遇到了同样的问题。
    • 目前,不需要firebase-admin;它只适用于firebase
    【解决方案2】:

    我很难找到正确的导入/要求。这对我有用!!

    const firebaseAdmin = require('firebase-admin');
    //other code ....
    
    //assigning value to myLocation attribute
    let newObject = {
        otherAttribute:"otherAttributeValue",
        myLocation: new firebaseAdmin.firestore.GeoPoint(latitude,longitude)
    }
    

    【讨论】:

      【解决方案3】:

      简单使用:

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

      【讨论】:

        【解决方案4】:

        对于使用“google/cloud-firestore”包的 PHP

        你必须使用这个类Google\Cloud\Core\GeoPoint

        示例:

        <?php
        
        $geoPoint = new \Google\Cloud\Core\GeoPoint($latitude , $longitude);
        $data = ['geopoint' => $geoPoint];
        

        【讨论】:

          【解决方案5】:
           GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
              _firestore
                  .collection('locations')
                  .add({'name': 'random name', 'position': geoFirePoint.data}).then((_) {
                print('added ${geoFirePoint.hash} successfully');
              });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-22
            • 1970-01-01
            • 1970-01-01
            • 2021-05-28
            • 1970-01-01
            • 2020-07-05
            • 1970-01-01
            • 2018-03-19
            相关资源
            最近更新 更多