我希望尽可能准确地找到用户的当前纬度和经度,并在MainActivity中的TextView上显示此数据。但是,应用程序始终返回0.0,0.0作为纬度和经度。
我已经尝试修复AndroidManifest,并手动为应用程序提供适当的位置权限。
public class MainActivity extends AppCompatActivity {
TextView mTextViewLocation ;
boolean permission ;
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
private double Latitude ;
private double Longitude ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupGPS();
mTextViewLocation = (TextView)findViewById(R.id.textViewLocation);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(60000);
mLocationRequest.setFastestInterval(20000);
mLocationRequest.setMaxWaitTime(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null){
Log.d("Location: GPS","off");
return;
}
for (Location location : locationResult.getLocations()) {
Log.d("locations : " ,location.getLatitude()+"");
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}
}
};
String s = Latitude + " " + Longitude ;
mTextViewLocation.setText(s);
}
public void setupGPS() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
if(PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)) {
permission=true;
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback,null);
}
else {
permission=false;
AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Permission Denied");
alert.setMessage("You need to enable location permissions for this app.");
alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
permission = true ;
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
608);
}
});
alert.setNegativeButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
permission=false;
}
});
alert.create().show();
}
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
607);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
}
我希望这在纬度和经度方面显示我的精确位置,但它现在只显示“0.0 0.0”。
这些是我在AndroidManifest中为应用程序提供的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
我还在我的应用程序的Build.gradle中包含了这个:
implementation 'com.google.android.gms:play-services-location:16.0.0'
String s = Latitude + " " + Longitude; mTextViewLocation.setText(s);
是在qazxsw poi方法之外回电话。由于在onLocationResult()
方法之前调用了qazxsw poi,因此在文本视图中你得到了错误的valuew。
以下是获取设备位置的步骤:
mTextViewLocation.setText(s);
onLocationResult()