繁体   English   中英

ChildEventListener 方法未调用 Firebase

[英]ChildEventListener methods not called Firebase

我想在 map 活动中添加标记,从 Firebase 实时数据库中检索到 position。

我尝试在onCreate()OnMapReady()方法中实现addChildEventListener() ,并且当用户登录时,尽管没有错误,但它们都没有工作,我还使用 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();
    }
}

这是我的数据库

数据库

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

虽然它作为一个节点存在,但我应该从参考中删除它,如果有人能解释一下吗?

提前致谢...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM