Java 阅读longlat data realtime Firebase并将其实现映射到Android Studio

Java 阅读longlat data realtime Firebase并将其实现映射到Android Studio,java,android,google-maps,firebase,firebase-realtime-database,Java,Android,Google Maps,Firebase,Firebase Realtime Database,你能帮我修一下吗 我想从Firebase实时读取数据并在地图上显示标记。longlat标记数据是从firebase实时读取的,而不是从代码读取的。Firebase更改地图中的数据也会更改 这是我的代码: public class mapmotor1 extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private FirebaseDatabase firebaseDatabase;

你能帮我修一下吗

我想从Firebase实时读取数据并在地图上显示标记。longlat标记数据是从firebase实时读取的,而不是从代码读取的。Firebase更改地图中的数据也会更改

这是我的代码:

public class mapmotor1 extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private static final int LOCATION_REQUEST = 500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapmotor1);

    // 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);

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("proyek-akhir-c93d7").child("1");

}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("proyek-akhir-c93d7").child("1");
   **//this is replaced what**
    LatLng device = new LatLng (-6.97455, 107.6326);

    mMap.addMarker(new MarkerOptions().position(device).title("Here"));

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(device, 15));
    mMap.animateCamera(CameraUpdateFactory.zoomIn());
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}

@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case LOCATION_REQUEST:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                mMap.setMyLocationEnabled(true);
            }
            break;
    }
}

假设
proyek-akhir-c93d7
是Firebase根目录,要获取
1
节点下的纬度和经度,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference oneRef = rootRef.child("1");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        double latitude = dataSnapshot.child("latitude").getValue(Double.class);
        double longitude = dataSnapshot.child("longitude").getValue(Double.class);
        //Do what you need to do with the latitude and longitude
        Log.d("TAG", latitude + ", " + longitude);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
oneRef.addListenerForSingleValueEvent(valueEventListener);
logcat中的输出将为:

-6.97455, 107.6326

“oneRef.addListenerForSingleValueEvent(valueEventListener);”代码放在哪里?下面是public void onCancelled或dataonchange?不,就在我的代码中看到的地方,就在最后一个大括号和分号之后,对吗?它有用吗?