我在Firebase上有一个包含位置的数据库。我想在用户启动地图活动时将此位置显示为标记群集。
启动该活动时会显示地图,但我无法从Firebase获取任何标记以显示为群集。
我在下面添加了我的代码:
private void displayLocations() {
Firebase myFirebaseRef = new Firebase("MY_URL");
// Get only latest logged locations - since 'START' button clicked
Query queryRef = myFirebaseRef.orderByChild("city");
// Add listener for a child added at the data at this location
queryRef.addChildEventListener(new ChildEventListener() {
LatLngBounds bounds;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Map data = (Map) dataSnapshot.getValue();
//String timestamp = (String) data.get("timestamp");
// Get recorded latitude and longitude
Map mCoordinate = (HashMap)data.get("address");
double latitude = (double) (mCoordinate.get("lat"));
double longitude = (double) (mCoordinate.get("lng"));
// 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("Pub")
.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 onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}