不称为Firebase的ChildEventListener方法

时间:2019-11-05 23:36:03

标签: android firebase firebase-realtime-database

我想在从Firebase实时数据库中检索位置的地图活动中添加标记。

我尝试在addChildEventListener()onCreate()方法中实现OnMapReady(),并且当用户登录时,尽管没有错误,但它们都不起作用,我还使用logcat来显示位置坐标,但其值未显示。

    email = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("email", "");
    password = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("password", "");
    name = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("name", "");
    gender = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("gender", "");
    age = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("age", "");
    mobileNumber = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("mobileNumber", "");
    if (email == "" || password == "" || name == "" || mobileNumber == "" || age == "" || gender == "") {
        startActivity(new Intent(this, WelcomeActivity.class));
        return;
    }

    setContentView(R.layout.activity_maps);
    getSupportActionBar().setTitle(R.string.app_name);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseSignIn();

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedVideoAd.setRewardedVideoAdListener(this);
    rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    findNurseButton = findViewById(R.id.find_nurse);
    findNurseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            findNurseButton.setText("finding nearby nurses");

        }
    });

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    assert mapFragment != null;
    mapFragment.getMapAsync(this);

    getLocationPermission();
    createLocationRequest();
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            else {
                try {
                    for (Location location : locationResult.getLocations()) {
                        // Update UI with location data
                        // ...
                        sendNurseRequest();
                        lastLocation = location;
                        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(16f));
                        Log.i("location updates: ", "latitude: " + location.getLatitude() + " ,longitude: " + location.getLongitude());
                        userReference.child("auth id").setValue(userID);
                        userReference.child("latitude").setValue(location.getLatitude());
                        userReference.child("longitude").setValue(location.getLongitude());
                    }
                }
                catch (NullPointerException e) {

                }
            }
        }
    };
    mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

    DatabaseReference nurseRef = FirebaseDatabase.getInstance().getReference().child("nurses available/2");
    nurseRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            LatLng latLng = new LatLng(Double.parseDouble(dataSnapshot.child("latitude").getValue().toString()), Double.parseDouble(dataSnapshot.child("longitude").getValue().toString()));
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.i("latslongs ", "firebase latitude:" + dataSnapshot.child("latitude").getValue() + "firebase longitude" + dataSnapshot.child("longitude").getValue());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.e("database error", databaseError.getMessage());
        }
    });
}

/**
 * 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;
   mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    if (locationPermissionGranted && checkInternet()) {
        getDeviceLocation();
    }
}

这是我的数据库

Database

1 个答案:

答案 0 :(得分:0)

DatabaseReference nurseRef = FirebaseDatabase.getInstance().getReference().child("nurses available/2"); // just remove the last child /2

尽管它作为一个节点存在,但是如果有人可以解释,我应该将其从参考文献中删除?

提前谢谢...