【发布时间】:2017-01-12 17:29:29
【问题描述】:
我有 POJO 类,我将 Firebase 数据库中的所有值分配给这个类,以便我可以返回纬度和经度坐标的双精度值。我检索这些值的方法只为所有纬度和经度值返回 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());
}
});
}
【问题讨论】:
标签: android google-maps firebase-realtime-database pojo