从Firebase数据库返回纬度和经度坐标的所有值

时间:2017-01-12 17:29:29

标签: android google-maps firebase-realtime-database pojo

我有POJO类,我将Firebase数据库中的所有值分配给此类,以便我可以返回纬度和经度坐标的double值。我检索这些值的方法仅返回所有纬度和经度值的0.0

我在下面添加了我的代码:

POJO班级

public class LocationModel {

private String id;
private String address;
private String image;
private String lat;
private String lng;
private String desc;


public LocationModel() {
}

public LocationModel(String id, String address, String image, String lat, String lng, String desc) {
    this.id = id;
    this.address = address;
    this.image = image;
    this.lat = lat;
    this.lng = lng;
    this.desc = desc;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}


public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}


public String getLat() {
    return lat;
}

public double setLat(String lat) {
    this.lat = lat;
    return 0;
}

public String getLng() {
    return lng;
}

public double setLng(String lng) {
    this.lng = lng;
    return 0;
}

我的方法显示所有纬度和经度坐标并显示为标记簇。

private void displayLocations() {
    DatabaseReference mapsrefrence=FirebaseDatabase.getInstance().getReference().child("places");
    mapsrefrence.child("testlocation").addListenerForSingleValueEvent(

            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.hasChildren()) {
                        @SuppressWarnings("unchecked")

                        LatLngBounds bounds;
                        LatLngBounds.Builder builder = new LatLngBounds.Builder();

                        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                            // TODO: handle the post

                            LocationModel location = postSnapshot.getValue(LocationModel.class);
                            LocationModel availableLocation = new LocationModel();
                            double latitude = availableLocation.setLat(location.getLat());
                            double longitude = availableLocation.setLng(location.getLng());
                            Log.i(TAG, "lat"+latitude);

                            // Create LatLng for each locations
                            LatLng mLatlng = new LatLng(latitude, longitude);

                            // Make sure the map boundary contains the location
                            builder.include(mLatlng);
                            bounds = builder.build();

                            // Add a marker for each logged location
                            MarkerOptions mMarkerOption = new MarkerOptions()
                                    .position(mLatlng)
                                    .title("Clubs")
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_map));
                            Marker mMarker = mMap.addMarker(mMarkerOption);
                            markerList.add(mMarker);

                            // Zoom map to the boundary that contains every logged location
                            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,
                                    MAP_ZOOM_LEVEL));
                        }


                    }

                    }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                }
            });
}

1 个答案:

答案 0 :(得分:1)

这部分看起来不对:

LocationModel location = postSnapshot.getValue(LocationModel.class);
LocationModel availableLocation = new LocationModel();
double latitude = availableLocation.setLat(location.getLat());
double longitude = availableLocation.setLng(location.getLng());
Log.i(TAG, "lat"+latitude);

您将latitudelongitude变量设置为setLat()setLng()的返回值,并且这两种方法每次都返回0。

您还在创建一个不必要的额外LocationModel实例。

这是你真正想要的:

LocationModel location = postSnapshot.getValue(LocationModel.class);
double latitude = location.getLat();
double longitude = location.getLng();
Log.i(TAG, "lat"+latitude); 
相关问题