gpt4 book ai didi

java - 如何在 Firebase 中保存位置信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:25 25 4
gpt4 key购买 nike

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

在他们的博文中,GeoFire goes Mobile , 他们展示了数据的样子 - 但我如何获得 location字段填充?

enter image description here

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

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

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


alanRef.setValue(obj);

我试过了 location成为List<String> ,但这没有用——位置字段如下所示:

enter image description here

编辑:经过更多研究,发现 this blog post by Google但它们也保存为 key latitude1 and 经度 . This probably was written before 引入了 GeoFire。

最佳答案

GeoFire for Java project has a great README ,涵盖(除其他外)setting location data :

In GeoFire you can set and query locations by string keys. To set a location for a key simply call the setLocation() method. The method is passed a key as a string and the location as a GeoLocation object containing the location's latitude and longitude:

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

To check if a write was successfully saved on the server, you can add a GeoFire.CompletionListener to the setLocation() call:

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!");
}
}
});

To remove a location and delete it from the database simply pass the location's key to removeLocation:

geoFire.removeLocation("firebase-hq");

关于java - 如何在 Firebase 中保存位置信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com