读取longlat数据实时Firebase及其实现以映射Android Studio

时间:2018-08-01 06:48:57

标签: java android google-maps firebase firebase-realtime-database

您能帮我解决这个问题吗?

My Firebase

我想从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;
    }
}

1 个答案:

答案 0 :(得分:0)

假设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