将多个 gps 位置保存到同一个 firebase 实时数据库
Saving multiple gps locations to same firebase real-time database
按下按钮时,我已成功将经度和纬度坐标存储到 Firebase 实时数据库中。如果 phone 的位置发生变化,当前数据库将覆盖坐标。但是,我想将新坐标追加到数据库中而不覆盖以前保存的坐标。
我曾尝试将其中一个坐标字符串作为子项传递,但数据库只接受 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);
}
});
坐标class:
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() method, it means that each time you call this method it overrides the data at the existing location. If you want to have different values for the coordinates under the great
node, then you should consider using the 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
那么就可以简单的读取数据了:
- How can I store and retrieve latitude and longitude values from Firebase?
按下按钮时,我已成功将经度和纬度坐标存储到 Firebase 实时数据库中。如果 phone 的位置发生变化,当前数据库将覆盖坐标。但是,我想将新坐标追加到数据库中而不覆盖以前保存的坐标。
我曾尝试将其中一个坐标字符串作为子项传递,但数据库只接受 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);
}
});
坐标class:
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() method, it means that each time you call this method it overrides the data at the existing location. If you want to have different values for the coordinates under the great
node, then you should consider using the 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
那么就可以简单的读取数据了:
- How can I store and retrieve latitude and longitude values from Firebase?