我在两个标记之间画一条路线,我想保存那条路线。为此,我在Firebase数据库中保存了包含lat和lng的ArrayList。但是我在检索航点时遇到了问题。这是我插入的方式:
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
for (int i = 0; i < result.size(); i++) {
points = new ArrayList();
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = result.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.color(Color.RED);
lineOptions.geodesic(true);
database.child("Route").child("route").setValue(points);
}
mMap.addPolyline(lineOptions);
}
检索数据时,我尝试执行以下操作:
userRef.child("Route").child("route).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList ids = null;
for (DataSnapshot childData : dataSnapshot.getChildren()) {
ids.add(childData.getValue());
}
}
答案 0 :(得分:1)
class Route {
private ArrayList<Location> locations;
@PropertyName("route")
public ArrayList<Location> getLocations() {
return locations;
}
@PropertyName("route")
public void setLocations(ArrayList<Locations> locations) {
this.locations = locations;
}
}
class Location{
private Double latitude;
private Double longitude;
//getter and setter for lat - long
}
然后你可以将它们用作
public void onDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
}
现在您可以根据需要使用检索到的数据。
编辑:工作代码:
public class RouteActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
FirebaseDatabase.getInstance().getReference("Route").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Route route = dataSnapshot.getValue(Route.class);
for(Location location : route.getLocations() ) {
Log.d("stackoverflow", location.getLatitude()+","+location.getLongitude());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class Route {
private ArrayList<Location> locations;
public Route() {
}
@PropertyName("route")
public ArrayList<Location> getLocations() {
return locations;
}
@PropertyName("route")
public void setLocations(ArrayList<Location> locations) {
this.locations = locations;
}
}
public static class Location{
private Double latitude;
private Double longitude;
//getter and setter for lat - long
public Location() {
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}
}
输出:
D/stackoverflow: 55.39,10.33
D/stackoverflow: 65.39,13.33
D/stackoverflow: 35.39,16.33
D/stackoverflow: 85.39,12.33
D/stackoverflow: 25.39,17.33
D/stackoverflow: 57.39,61.33
答案 1 :(得分:0)
使用值创建此自定义类。
class MapRouteCords{
public Double latitude;
public Double longitude;
public MapRouteCords()
{
}
public MapRouteCords(Double latitude, Double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
使用OnDataChange Listener中的方法
collectcords((Map<String, Object>) dataSnapshot.getValue());
定义方法。
private void collectcords(Map<String, Object> mapcords) {
ArrayList<Double> alllats = new ArrayList<Double>();
ArrayList<Double> alllongs = new ArrayList<Double>();
for (Map.Entry<String, Object> entry : mapcords.entrySet()) {
Map singleGame = (Map) entry.getValue();
alllats.add((Double) singleGame.get("latitude"));
alllongs.add((Double) singleGame.get("longitude"));
}
答案 2 :(得分:0)
这是从数据库中获取它的好方法: 按照以下步骤操作,最后您将得到您想要的结果。
第1步: 创建一个类
路线
:包含所有属性和getter以及setter。并确保保持一个空的构造函数。
第2步 创建Route的ArrayList并使用
将其存储在数据库中ref.setValue(arrayListRoutes);
第3步 从Firebase数据库获取数据,假设Datasnapshot是ref节点上的快照。使用此 onDataChange(DataSnapshot dataSnapshot):
GenericTypeIndicator<ArrayList<Route>> indicator = new GenericTypeIndicator<ArrayList<Route>>(){};
ArrayList<Route> DataList = dataSnapshot.getValue(indicator);