我正在尝试将位置(纬度和经度)保存为 Firebase 中的键/字段之一。在他们的例子中SF车辆,它们确实展示了如何在存储信息后进行查询,但我的问题是如何首先保存。
在他们的博客文章中,GeoFire 走向移动,他们展示了数据的样子 - 但我如何得到它location
字段填充?
不过,我可以将其他类型的字符串保存到 Firebase 中。我只是使用下面的代码。
问题是应该使用什么数据类型location
场是?
Firebase ref = new Firebase("https://myfirebaselink.firebaseio.com/");
//User
alan = new User("Alan Turing", 1912);
alanRef.setValue(obj);
I tried location
成为一个List<String>
,但这不起作用——位置字段如下所示:
编辑:经过更多研究,发现谷歌的这篇博文但它们也保存为密钥latitude1 and
经度. This probably was written before
引入了 GeoFire`。
The GeoFire for Java 项目有一个很棒的自述文件,涵盖(除其他外)设置位置数据:
在 GeoFire 中,您可以通过字符串键设置和查询位置。要设置键的位置,只需调用setLocation()
方法。该方法以字符串形式传递键,以GeoLocation
包含位置纬度和经度的对象:
geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));
要检查写入是否已成功保存在服务器上,您可以添加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!");
}
}
});
要删除位置并将其从数据库中删除,只需将该位置的键传递给removeLocation:
geoFire.removeLocation("firebase-hq");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)