从Firebase数据库检索数据

从Firebase数据库检索数据

本文介绍了Android-从Firebase数据库检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制两个标记之间的路线,并且我想保存该路线.为此,我将包含lat和lng的ArrayList保存在Firebase数据库中.但是我在检索航路点时遇到问题.这是我插入的方式:

I am drawing a route between two markers, and I want to save that route. To do that I saved the ArrayList containing the lat and lng in the Firebase database. But I am having problems retrieving the waypoints. This is how I inserted:

  @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);
    }

检索数据时,我尝试执行以下操作:

When retrieving the data, I tried to do the following:

 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());
            }

        }

推荐答案

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);
}

现在,您可以根据需要使用检索到的数据.

Now you can use the data retrieved as you want.

工作代码:

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;
    }
}
}

这是我正在查询的Firebase节点

This is firebase node I am querying

输出: D/stackoverflow:55.39,10.33D/堆栈溢出:65.39,13.33D/堆栈溢出:35.39,16.33D/堆栈溢出:85.39,12.33D/堆栈溢出:25.39,17.33D/stackoverflow:57.39,61.33

这篇关于Android-从Firebase数据库检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:36