无法在我的Firebase

时间:2017-03-14 11:59:29

标签: android google-maps

我有自己的地图,在地图上签名后会显示然后获取我的坐标(lat,long)。我已将此代码放在Override方法onLocationChange(位置位置)中,这样每次用户走路或移动时,lat和long都会更新。问题是我无法将值存储在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();
        }

数据库(火力地堡)

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


                          }

    }

1 个答案:

答案 0 :(得分: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/

干杯