【问题标题】:Update Location data using android app in firebase database使用 Firebase 数据库中的 android 应用程序更新位置数据
【发布时间】:2017-03-04 05:59:29
【问题描述】:

我正在开发一个简单的 android 应用程序,我需要在其中创建地图和跟踪位置,就像 Uber 一样...我使用 firebase 作为我的数据库来存储用户的位置。 为了存储位置,我创建了一个在调用 MainActivity 时调用的服务 我面临的问题是该位置仅在数据库中更新一次,即当位置更改时,它不会在数据库中更新,因为我必须重新启动应用程序才能更新新位置。

这是我的位置服务中 onLocationChanged 方法的代码

public void onLocationChanged(Location location) {
Log.d("lam", "onLocationChanged:lam "+location.getLatitude()); //The log gets updated but not the values in database

Map<String,Object> taskMap=new HashMap<>();
taskMap.put("Latitude",location.getLatitude());
taskMap.put("Longitude",location.getLongitude());
try{                
    urlCurrenUser.updateChildren(taskMap);//urlCurrentUser is the path to database
   }catch(Exception e){}
                }

我也试过了

public void onLocationChanged(Location location) {
Log.d("lam", "onLocationChanged:lam "+location.getLatitude()); //The log gets updated but not the values in database
 try {
urlCurrenUser.child("Lattitude").setValue(location.getLattitude());
urlCurrenUser.child("Longitude").setValue(location.getLongitude());
}catch (Exception e){}

我在主要活动的 onCreate() 中调用了该服务

编辑 我认为我的 urlCurrentUser (对数据库的引用)中存在问题,当应用程序第一次尝试启动时,位置在数据库中得到更新,但是当位置发生变化并再次调用代码来更新新坐标时,它给出了一个异常,即

java.lang.NullPointerException:尝试调用虚拟方法 'com.firebase.client.Firebase com.firebase.client.Firebase.child(java.lang.String)' 在空对象上 参考

我只是不明白如何解决这个问题 这是我的数据库结构的快照 enter image description here

数据库参考代码

urlCurrenUser = new Firebase(URL).child("users").child(authData.getUid());

【问题讨论】:

    标签: android firebase


    【解决方案1】:

    关注最新的docs,您正在使用旧 API 版本。

    1 - 确保您已配置正确的 google 服务 JSON 文件 从 firebase 项目控制台下载的。

    2 - 默认情况下,对数据库的读写访问受到限制,因此 只有经过身份验证的用户才能读取或写入数据。开始 无需设置Authentication,您可以configure your rules 进行公共访问。这确实使您的数据库对 任何人,甚至是不使用您的应用程序的人,因此请务必限制您的 设置身份验证时再次使用数据库。

    3 - 准确地按照有据可查的article 进行书写和阅读 来自 firebase 数据库的数据。

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多