java - locationManager.getLastKnownLocation 在请求后返回 null
问题描述
我尝试通过 GPS 获取当前位置并显示它。
首先我初始化LocationManager,所以我总是在使用方法getLocation()
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, LocationListener {
private static final int REQUEST_LOCATION = 1;
private static int MY_PERMISSIONS_REQUEST_ACCESS;
TextView locationText;
LocationManager locationManager;
String lattitude,longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Button buttonLocate = (Button) findViewById(R.id.buttonLocate);
locationText = (TextView)findViewById(R.id.locationText);
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
buttonLocate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(locationManager != null) {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// ..DO SMTH
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
}
}
}
});
}
在getLocation()
我检查权限的方法中,如果他们被授予,我请求位置更新并尝试获取纬度和经度,这是我的问题,因为位置总是 null,所以我无法获得坐标!代码可能有什么问题,我该如何解决?...
void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
// REQUEST!
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
Location location;
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
// LOCATION IS ALWAYS NULL!!!
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
locationText.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else {
Toast.makeText(this, "Unble to Trace your location", Toast.LENGTH_SHORT).show();
}
}
}
解决方案
getLastKnownLocation返回设备的最后一个已知位置。如果没有缓存最后一个已知位置,它将返回 null。
getLastKnownLocation
当为空时,您需要请求位置更新。
为了获得位置更新,您必须实施onLocationChanged:
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
}
推荐阅读
- python - Google Dataproc Presto:如何使用 Python 运行查询
- go - 在 GUI 中从 for 循环中分配适当的值
- php - 滑块问题 - 如何在 php 滑块中支持图像和视频
- python - 获取字符串中数字的索引并提取数字前后的单词(不同语言)
- ios - SecureField 的 .newPassword 选择强密码不使用 SwiftUI 和 iOS14
- python - 当给定一个元组作为颜色参数时,为什么 pygame 的 draw rect 函数会抛出一个 ValueError?
- php - 具有多态关系的 Eloquent join 子句覆盖 id 列
- vue.js - Vue警告无法解析组件:离子图标
- gltf - glTF 模型在场景中是红色的
- java - 如何在 MongoDb 中结合 $in 和 max?