我想通过单击来从Firebase检索多个LatLng实际上我是从firebase提取单个LatLng。如何获取plz告诉我 这是地图活动代码
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference("Location");
ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Double latitude=dataSnapshot.child("latitude").getValue(Double.class);
Double longitude=dataSnapshot.child("longitude").getValue(Double.class);
LatLng location = new LatLng(latitude,longitude);
// Add a marker in Lahore and move the camera
// LatLng Lahore = new LatLng(31.588048, 74.315097);
//Different Map types
//mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
//mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
//mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.addMarker(new MarkerOptions().position(location).title("Shahi Qila"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,15.2f));
// mMap.setMyLocationEnabled(true);
// mMap.moveCamera(CameraUpdateFactory.newLatLng(Lahore));
//zoom
/*
CameraUpdate zoom= CameraUpdateFactory.zoomTo(20);
mMap.moveCamera(zoom);
mMap.animateCamera(zoom);*/
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
这是数据库结构
答案 0 :(得分:0)
您的代码可以,但是您必须检查
中接收的数据onDataChange
带有调试点。
并说说您运行应用程序时会发生什么 在评论中更新我
答案 1 :(得分:0)
首先创建模型以直接解析数据
public class MyLocation {
String des;
double latitude, longitude;
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public Long getLatitude() {
return latitude;
}
public void setLatitude(Long latitude) {
this.latitude = latitude;
}
public Long getLongitude() {
return longitude;
}
public void setLongitude(Long longitude) {
this.longitude = longitude;
}
}
然后像下面一样在DatabaseReference
上进行查询以获取数据
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot locationSnapshot: dataSnapshot.getChildren()) {
MyLocation myLocation = locationSnapshot.getValue(MyLocation.class);
LatLng location = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.addMarker(new MarkerOptions().position(location).title("Shahi Qila"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, 15.2f));
}
}
});