如何在 Firebase 中保存位置信息
how to save Location Info in Firebase
我正在尝试将位置(纬度和经度)保存为 Firebase 中的 keys/fields 之一。在他们的示例 SFVehicles 中,他们确实展示了信息存储后如何查询,但我的问题是首先如何保存。
在他们的博客 post、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 被引入。
从here看来
对象的类型是 GeoLocation ,如第 83 行。
GeoFire for Java project has a great README, that covers (amongst other) 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");
我正在尝试将位置(纬度和经度)保存为 Firebase 中的 keys/fields 之一。在他们的示例 SFVehicles 中,他们确实展示了信息存储后如何查询,但我的问题是首先如何保存。
在他们的博客 post、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 被引入。
从here看来 对象的类型是 GeoLocation ,如第 83 行。
GeoFire for Java project has a great README, that covers (amongst other) 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 aGeoLocation
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 thesetLocation()
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");