未从LocationListener类获取Latitude和Longitude



我的LocationListener有问题。为什么我的代码没有在onLocationChange((方法中获得纬度和经度坐标?它正在传递纬度和经度的坐标为0。有人能指出这段代码中的错误和需要做的更改吗?

public class Login extends AppCompatActivity implements LocationListener {
private EditText e_mail, pass_word;
FirebaseAuth mAuth;
FirebaseFirestore fStore;
String userID;
public static final String TAG = "TAG";
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
private double lat;
private double lng;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
e_mail = findViewById(R.id.emailLogin);
pass_word = findViewById(R.id.password);
Button btn_login = findViewById(R.id.btn_login);
Button btn_sign = findViewById(R.id.btn_signup);

fStore = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


btn_login.setOnClickListener(v -> {
String email= e_mail.getText().toString().trim();
String password=pass_word.getText().toString().trim();
if(email.isEmpty())
{
e_mail.setError("Email is empty");
e_mail.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
e_mail.setError("Enter the valid email");
e_mail.requestFocus();
return;
}
if(password.isEmpty())
{
pass_word.setError("Password is empty");
pass_word.requestFocus();
return;
}
if(password.length()<6)
{
pass_word.setError("Length of password is more than 6");
pass_word.requestFocus();
return;
}
mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(task -> {
if(task.isSuccessful())
{
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Registration")
.setMessage("What do you want to register as ? ")
.setCancelable(false)
.setPositiveButton("Organization", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent organization = new Intent(Login.this,OrganizationPage.class);
startActivity(organization);
Login.this.finish();
}
})
.setNegativeButton("Volunteer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
uploadData();
Intent volunteer = new Intent(Login.this,VolunteerPage.class);
startActivity(volunteer);
Login.this.finish();}
});
//Creating dialog box
AlertDialog dialog  = builder.create();
dialog.show();
}
else
{
Toast.makeText(Login.this,
"Please Check Your login Credentials",
Toast.LENGTH_SHORT).show();
}
});

});
btn_sign.setOnClickListener(v -> {
startActivity(new Intent(Login.this, Register.class));
});
}

@Override
public void onLocationChanged(@NonNull Location location) {
lat =location.getLatitude();
lng = location.getLongitude();
Log.v("Activity location ", String.valueOf(lat)+"latitude of volunteer");
Toast.makeText(this,lat+" Works "+lng,Toast.LENGTH_SHORT).show();
}
public void uploadData(){
String name = username;
double latitude = lat;
double longitude = lng;
Log.v("Activity location ", String.valueOf(latitude));

userID = mAuth.getCurrentUser().getUid();
CollectionReference collectionReference = fStore.collection("Volunteer data");
Map<String,Object> user = new HashMap<>();
user.put("timestamp", FieldValue.serverTimestamp());
user.put("name",name);
user.put("volunteerLatitude",latitude);
user.put("volunteerLongitude",longitude);
user.put("userid",userID);
collectionReference.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(),"Successfully Registered as Volunteer",Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Error!",Toast.LENGTH_SHORT).show();
}
});
}
}

您需要在manifest.xml中添加位置,如下所示:

<manifest ... >
<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

最新更新