getLastKnownLocation 方法始终返回 null
我的固定位置有问题。我使用 getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 但总是返回 null,我已经在您的 AndroidManifest.xml
中设置了权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
并在“设置”中启用-->位置和安全 -->通过网络定位。
TextView locationShow = (TextView) this.findViewById(R.id.location);
double latitude = 0.0, longitude = 0.0;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
else {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " +
location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
locationShow.setText("经度:" + latitude + "纬度:" + longitude);
我发现其他应用程序可以正确显示位置,所以也许我的代码有问题。
I have a problem to fixed position. I use getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
but always return null, i have set the permissions in your AndroidManifest.xml
.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
and enabled in Settings --> Location and Security --> location through network.
TextView locationShow = (TextView) this.findViewById(R.id.location);
double latitude = 0.0, longitude = 0.0;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
else {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " +
location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
locationShow.setText("经度:" + latitude + "纬度:" + longitude);
I find that other apps can show the location correctly, so maybe there something wrong with my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getLastKnownLocation()
给出最后一个有效的缓存位置。您正在尝试从网络提供商处获取缓存的位置。您必须等待几分钟才能获得有效的修复。由于网络提供商的缓存为空,因此您显然会得到一个
null
。getLastKnownLocation()
give the last valid cached location.You are trying to get the cached location from the Network Provider. You have to wait for a few minutes till you get a valid fix. Since the Network Provider's cache is empty, you are obviously getting a
null
there.