我想在从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,"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,"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();
}
}
这是我的数据库