【问题标题】:how to save Location Info in Firebase如何在 Firebase 中保存位置信息
【发布时间】:2016-04-10 18:52:20
【问题描述】:

我正在尝试将位置(纬度和经度)保存为 Firebase 中的键/字段之一。在他们的示例SFVehicles 中,他们确实展示了如何在信息存储后进行查询,但我的问题是我首先如何保存。

在他们的博客文章 GeoFire goes Mobile 中,他们展示了数据的外观 - 但我如何填充 location 字段?

不过,我可以将其他类型的字符串保存到 Firebase。我只是使用下面的代码。 问题是location 字段应该是什么数据类型?

        Firebase ref = new Firebase("https://myfirebaselink.firebaseio.com/");

       //User 
       alan = new User("Alan Turing", 1912);


        alanRef.setValue(obj);

我尝试将location 设为List<String>,但这不起作用——位置字段如下所示:

编辑:在更多的研究中,发现了this blog post by Google,但它们也保存为密钥latitude1 and longitude. This probably was written before GeoFire` 被引入。

【问题讨论】:

  • 您在问题中包含了 JSON 树的图片。请将其替换为作为文本的实际 JSON,您可以通过单击 Firebase 数据库中的“导出”按钮轻松获取该文本。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,一般来说这只是一件好事。
  • googlegeodeveloper 博客文章不使用 GeoFire。因此,如果您想使用 GeoFire,这与您无关。

标签: java android firebase geofire


【解决方案1】:

GeoFire for Java project has a great README,包括(除其他外)setting location data

在 GeoFire 中,您可以通过字符串键设置和查询位置。要设置键的位置,只需调用setLocation() 方法。该方法以字符串形式传递一个键,并将位置作为 GeoLocation 对象传递,该对象包含该位置的纬度和经度:

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

要检查写入是否成功保存在服务器上,您可以在setLocation() 调用中添加GeoFire.CompletionListener

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {
    @Override
    public void onComplete(String key, FirebaseError error) {
        if (error != null) {
            System.err.println("There was an error saving the location to GeoFire: " + error);
        } else {
            System.out.println("Location saved on server successfully!");
        }
    }
});

要删除一个位置并将其从数据库中删除,只需将位置的密钥传递给 removeLocation:

geoFire.removeLocation("firebase-hq");

【讨论】:

  • 谢谢,readme 很好,把所有操作都解释清楚了。傻我错过了,谢谢。
【解决方案2】:

here看来 对象的类型是 GeoLocation ,如第 83 行。

【讨论】:

  • 链接已过时:(
猜你喜欢
  • 2022-11-21
  • 2015-04-06
  • 2021-11-30
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多