如何在Firebase中保存位置信息

编程入门 行业动态 更新时间:2024-10-18 12:21:11
本文介绍了如何在Firebase中保存位置信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图保存位置(如latitide和longitude)作为Firebase中的一个键/字段。在他们的示例

我可以保存其他类型的字符串Firebase虽然。我只是使用下面的代码。 问题是 位置字段是什么数据类型?

Firebase ref = new Firebase(myfirebaselink.firebaseio/); $ b $ //用户 alan =新用户(Alan Turing,1912); alanRef.setValue(obj);

我试过 location c $ c> List< String> ,但没有奏效 - 位置字段如下所示:

编辑:在更多的研究中,发现这个由Google发布的博文,但它们也保存为 latitude1和经度。这可能是在 GeoFire`被引入之前写的。 GeoFire for Java项目有一个很棒的自述文件,涵盖(除其他外)设置位置数据:

在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(将位置保存到GeoFire: + error; } else { System.out.println(成功保存在服务器上的位置!); } } });

要删除某个位置并将其从数据库中删除,只需传递位置key to removeLocation:

geoFire.removeLocation(firebase-hq);

I am trying to save location (so latitide and longitude) as one of the keys/fields in Firebase. In their example SFVehicles, they do show how to query once the information is stored but my problem is how do i save in the first place.

In their blog post, GeoFire goes Mobile, they are showing how the data would look like - but how do I get that location field populated?

I am able to save other types of strings to the Firebase though. I just use the code below. Question is What data type should the location field be?

Firebase ref = new Firebase("myfirebaselink.firebaseio/"); //User alan = new User("Alan Turing", 1912); alanRef.setValue(obj);

I tried location to be a List<String>, but that did not work -- the location field looked like below:

Edit: On more research, found this blog post by Google but they are also saving as keys latitude1 andlongitude. This probably was written beforeGeoFire` was introduced.

解决方案

The 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中保存位置信息

本文发布于:2023-10-08 01:26:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   如何在   信息   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!