【发布时间】:2018-01-30 19:10:04
【问题描述】:
正如标题所说,我是 android 新手。我正在尝试通过我在 android studio 中的地图上的标记显示所有用户的位置。问题是,只有我的 Firebase 数据库的最后一个用户是唯一显示的用户标记。我想显示所有用户的所有标记。
这是我从 Firebase 数据库中获取所有用户(标记)的代码
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
refDatabase = FirebaseDatabase.getInstance().getReference().child("users");
refDatabase.orderByChild("availability").equalTo("yes").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
//LatLng newLocation = new LatLng(
// ds.child("latitude").getValue(Double.class),
//ds.child("longtitude").getValue(Double.class));
UserInformation userInfo = ds.getValue(UserInformation.class);
if(userInfo.getExerciseType().equals(uExerciseType)){
double mylat = userInfo.getLatitude();
double mylong = userInfo.getLongtitude();
String exTitle = ds.child("exerciseType").getValue(String.class);
LatLng newLocation = new LatLng(mylat, mylong);
Log.d("New Location: ", newLocation.toString());
//refreshes the map
mMap.clear();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(newLocation);
markerOptions.title(exTitle);
markerOptions.visible(false);
Marker locationMarker = mMap.addMarker(markerOptions);
LatLng myLatLang = new LatLng(uLatt, uLongt);
Log.d("My Location: ", myLatLang.toString());
if (SphericalUtil.computeDistanceBetween(myLatLang, locationMarker.getPosition()) < 1000000) {
locationMarker.setVisible(true);
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLatLang, 10.2f));
}
}
}
}
然后,我的 Firebase 数据库如下所示:
【问题讨论】:
-
顺便说一句,我已经使用过这个解决方案,但仍然没有用。 stackoverflow.com/questions/43635994/… 和 stackoverflow.com/questions/45747796/…
-
为什么不使用 for(DatasnapShot ds : --- ) 来获取所有用户?
-
我正在使用它,它会获取所有用户,但问题是只有最后一个用户会显示在我的地图上。我希望显示所有用户
-
请不要在您的问题中编辑答案。如果您找到了答案,您应该将其作为答案发布在下方,然后将其标记为“已接受”。
标签: android google-maps firebase-realtime-database