我正在尝试从firebase检索纬度和经度数据到我的谷歌地图,但是当我运行应用程序时,标记不会显示。
这是我的代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private FirebaseDatabase tracker;
private DatabaseReference databaseLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
tracker = FirebaseDatabase.getInstance();
databaseLocation = tracker.getReference("lokasi3");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
databaseLocation.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("lokasi3").getChildren()) {
Lokasi3 lokasi3 = dataSnapshot.getValue(Lokasi3.class);
String latitude = child.child("latitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
double loclatitude = Double.parseDouble(latitude);
double loclongitude = Double.parseDouble(longitude);
LatLng cod = new LatLng(loclatitude, loclongitude);
googleMap.addMarker(new MarkerOptions().position(cod).title(""));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
在onCreate
中收听您的数据 databaseLocation.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("lokasi3").getChildren()) {
Lokasi3 lokasi3 = dataSnapshot.getValue(Lokasi3.class);
String latitude = child.child("latitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
double loclatitude = Double.parseDouble(latitude);
double loclongitude = Double.parseDouble(longitude);
LatLng cod = new LatLng(loclatitude, loclongitude);
googleMap1.addMarker(new MarkerOptions().position(cod).title(""));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
GoogleMap googleMap1;//make it global
@Override
public void onMapReady(final GoogleMap googleMap) {
googleMap1=googleMap;
}