Я пытаюсь показать все выводы из базы данных Firebase, но они не отображаются, как это исправить? Помогите мне, пожалуйста. В булавках есть расположение булавок, я добавил некоторое сообщение Toast в onKeyEntered, но оно также не работало. ,,,... ,... ,,,,,,,,,,,,,,,,,,,,... ,,,... ,,,...
Импортные пакеты
package com.asmfdn.helpme;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.firebase.geofire.GeoQuery;
import com.firebase.geofire.GeoQueryEventListener;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
Location mLocation;
LocationRequest mLocationRequest;
Button btnHelp;
LatLng requestLoc;
EditText etPinName;
private FusedLocationProviderClient fusedLocationProviderClient;
private SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
btnHelp = (Button)findViewById(R.id.buttonHelp);
etPinName = (EditText)findViewById(R.id.EditTextPinName);
btnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
pinClick();
}
catch (Exception e){
Toast.makeText(MapsActivity.this, "Please Wait.... Slow connection...", Toast.LENGTH_SHORT).show();
}
}
});
}
public void pinClick(){
final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("pins");
GeoFire geoFire = new GeoFire(ref);
if(TextUtils.isEmpty(etPinName.getText().toString())){
etPinName.setError("Please enter the pin message!");
return;
}
geoFire.setLocation(userId, new GeoLocation(mLocation.getLatitude(), mLocation.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
String uID = FirebaseAuth.getInstance().getCurrentUser().getUid();
String pinName = etPinName.getText().toString();
DatabaseReference pinNameRef = FirebaseDatabase.getInstance().getReference("pins").child(uID).child("pin");
pinNameRef.child("PinText").setValue(pinName);
// requestLoc = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
// mMap.addMarker(new MarkerOptions().position(requestLoc).title("Help"));
btnHelp.setText("Pinned");
}
});
}
/**
* 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;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);
}else{
checkLocationPermission();
}
}
}
LocationCallback mLocationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for(Location location:locationResult.getLocations()){
if(getApplicationContext()!=null) {
mLocation = location;
LatLng latLng = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
if(!bolFind){
Toast.makeText(MapsActivity.this, "Working", Toast.LENGTH_SHORT).show();
findPins();
}
}
}
}
};
private void checkLocationPermission() {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
new AlertDialog.Builder(this)
.setTitle("Give Permission")
.setMessage("Give Permission")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
})
.create()
.show();
}
else {
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 1:{
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);
}
else{
}
}
else {
Toast.makeText(getApplicationContext(), "Please provide the permission", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Boolean bolFind =false;
List<Marker>markerList = new ArrayList<Marker>();
public void findPins(){
bolFind = true;
DatabaseReference pinsRef = FirebaseDatabase.getInstance().getReference().child(("pins"));
GeoFire geoFire = new GeoFire(pinsRef);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mLocation.getLatitude(), mLocation.getLongitude()), 10000);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
for(Marker markerIt:markerList){
if(markerIt.getTag().equals(key)){
return;
}
}
//mMap.addMarker(new MarkerOptions().position(new LatLng(7.3851933, 81.8414815)).title("avx"));
LatLng markerLocation = new LatLng(location.latitude, location.longitude);
Marker pinPoint = mMap.addMarker(new MarkerOptions().position(markerLocation).title(key));
pinPoint.setTag(key);
markerList.add(pinPoint);
}
@Override
public void onKeyExited(String key) {
for(Marker markerIt:markerList){
if (markerIt.getTag().equals(key)) {
markerIt.remove();
markerList.remove(markerIt);
return;
}
}
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
for(Marker markerIt:markerList){
if(markerIt.getTag().equals(key)){
markerIt.setPosition(new LatLng(location.latitude, location.longitude));
}
}
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
}