Reputation: 29
I have POJO class which I assign all values from Firebase Database to this class so that I can return the double value of latitude and longitude coordinates. My method to retrieve these values only returns 0.0
for all latitude and longitude values.
I have added my codes below:
POJO Class
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;
}
My method to display all latitude and longitude coordinates and display as cluster of markers.
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());
}
});
}
Upvotes: 0
Views: 1252
Reputation: 43322
This section looks wrong:
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);
You're setting the latitude
and longitude
variables to the return value of setLat()
and setLng()
, and both of those methods return 0 every time.
You're also creating an unnecessary extra LocationModel instance.
This is what you actually want:
LocationModel location = postSnapshot.getValue(LocationModel.class);
double latitude = location.getLat();
double longitude = location.getLng();
Log.i(TAG, "lat"+latitude);
Upvotes: 1