将多个gps位置保存到同一个火场实时数据库中



按下按钮时,我已成功将纵向和横向坐标存储到Firebase实时数据库中。如果手机的位置发生变化,当前数据库会覆盖坐标。但是,我希望在不覆盖以前保存的坐标的情况下,将新坐标附加到数据库中。

我尝试将其中一个坐标字符串作为子字符串传递,但是数据库只接受a-z字母。有五个单独的按钮,每个按钮都记录用户的情绪和该情绪的位置。

btnGreat = (ImageButton) view.findViewById(R.id.btnGreat);
btnGreat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rootNode = FirebaseDatabase.getInstance();
reference =  rootNode.getReference("Location");
String latitude = Latitude.getText().toString();
String longitude = Longitude.getText().toString();
Coordinates coordinates = new Coordinates(latitude, longitude);
reference.child("great").setValue(coordinates);
}
});

坐标类别:

public class Coordinates {
String latitude, longitude;
public Coordinates() {
}
public Coordinates(String latitude, String longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
}

由于您使用的是setValue((方法,这意味着每次调用此方法时,它都会覆盖现有位置的数据。如果您希望great节点下的坐标具有不同的值,那么您应该考虑使用如下的push((方法:

reference.child("great").push().setValue(coordinates);

这将创建一个看起来像这样的数据库结构:

Firebase-root
|
--- Location
|
--- great
|
--- $pushedId
|      |
|      --- latitude: 1.11
|      |
|      --- longitude: 2.22
|
--- $pushedId
|
--- latitude: 3.33
|
--- longitude: 4.44

然后你可以简单地读取数据:

  • 如何从Firebase存储和检索纬度和经度值

最新更新