【问题标题】:Can't store the values of longitude and latitude in my Firebase无法在我的 Firebase 中存储经度和纬度值
【发布时间】:2017-03-14 11:59:28
【问题描述】:

我有自己的地图,登录后会显示地图,然后获取我的坐标(纬度、经度)。我已将此代码放在 Override 方法 onLocationChange(Location location) 中,这样每次用户在任何地方行走或移动时,都会更新纬度和经度。问题是我无法将值存储在我的 Firebase 中。

try {


            DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();

            LoginModel model = new LoginModel();

            model.setLatitude(location.getLatitude());
            model.setLongitude(location.getLongitude());
            myRef.setValue(model, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                    if (databaseError != null) {
                        Toast.makeText(Location.this, "Data could not be saved " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(Location.this, "Data saved successfully.", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            Toast.makeText(this, "Latitude :" + location.getLatitude() + " Longitude :" + location.getLongitude() , Toast.LENGTH_SHORT).show();

        }catch (Exception e){
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }

数据库(Firebase)

User{
      -KfBWefAFItacoMK5hF9{
                            email: "justinseneres29@gmail.com"
                            latitude: 0
                            longitude: 0
                            mobile: "09125516612"
                            name: "Phantom"
                            pass: "*********"


                          }

    }

【问题讨论】:

  • 首先,我建议不要在firebase节点内存储任何重要数据,直到您完全熟悉firebase数据库规则。 (邮件、手机、密码……)。接下来,您的日志和 toast 的输出是什么?你确定,纬度和经度是正确的不同于零吗?然后,尝试使用 updatemap 方法,否则您将始终重新创建完整的键值条目集。
  • @JacksOnF1re 我的祝酒词“数据无法保存权限被拒绝”的输出以及纬度和对数的值
  • 啊,您的帖子中缺少该信息 :) 检查您的数据库规则。转到firebase控制台,选项卡数据库并单击“规则”。现在检查规则是否足以满足您的开发帐户。出于测试目的,将规则设置为“read=true, write=true”并再次测试。现在上班了吗? (之后重置规则)
  • @JacksOnF1re 谢谢先生,它现在可以工作了。你知道如何调用嵌套节点吗?就像我的数据库一样?因为我想在那里存储经度和纬度的值。

标签: android google-maps


【解决方案1】:

权限被拒绝

啊,您的帖子中缺少该信息 :) 检查您的数据库规则。转到firebase控制台,选项卡数据库并单击“规则”。现在检查规则是否足以满足您的开发帐户。出于测试目的,将规则设置为“read=true, write=true”并再次测试。现在上班了吗? (之后重置规则)

你知道如何调用嵌套节点吗?

尝试使用方法rootReference.updateChildren(Map<String, Object>);一次更新多个节点和子节点。该地图包含<KEY, OBJECT>,其中key 是一个连接键,例如"User/KfBWefAFItacoMK5hF9/latitude"object 是您要输入的值(这里是纬度的浮点数)。

这种方法的优点是更新节点中特定的键值对,而不需要更新整个节点。即您可以更新特定用户的邮件值,而不会覆盖经度和纬度。

你也可以通过调用直接更新一个值

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("your_root_here_or_empty");

ref.child("childNodeName").child("subChildNodeName").setValue(yourObject, yourListener);

我也强烈推荐阅读: https://firebase.google.com/docs/database/android/start/

干杯

【讨论】:

  • 我试着改变规则先生,我的规则是这样的"Users":{ ".read":true, ".write":true, "Name":{ ".read": true, ".write":true, "name":{ ".read": "auth!=null", ".write": "auth!=null" }, "email":{ ".read": "auth!=null", ".write": "auth != null" }, "pass":{ ".read": "auth!=null", ".write": "auth!=null" }, "mobile":{ ".read": "auth!=null", ".write": "auth!=null" }, } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2020-07-18
  • 2010-11-16
  • 2011-09-05
相关资源
最近更新 更多