getLastKnownLocation返回null

我已经阅读了一些关于这方面的问题,但是我没有find我需要的答案。

所以,案件是,我有我的地图设置,我想抢我当前的GPS位置。 我已经检查,我的variables不是NULL,但我的结果来自:

getLastKnownLocation(provider, false); 

虽然给我空,所以这是我需要帮助的地方。 我已经添加了COARSE + FINE位置的权限。 但是我通常会为我的手机禁用所有types的networking数据,因为我对手机账单中不可预知的数据stream开支并不满意。 所以我只有WiFi启用和连接为这个testing。

还有什么我需要启用,为了这是可能的? 我会认为WiFi应该足够了吗?

使用此方法获取最后一个已知位置:

 LocationManager mLocationManager; Location myLocation = getLastKnownLocation(); private Location getLastKnownLocation() { mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); List<String> providers = mLocationManager.getProviders(true); Location bestLocation = null; for (String provider : providers) { Location l = mLocationManager.getLastKnownLocation(provider); if (l == null) { continue; } if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { // Found best last known location: %s", l); bestLocation = l; } } return bestLocation; } 

您正尝试从networking提供商获取caching的位置。 你必须等待几分钟,直到你得到一个有效的修复。 由于networking提供商的caching是空的,你显然在那里得到一个空的..

添加此代码

 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } 

之前的function:

locationManagerObject.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new LocationListener())以及函数: locationManagerObject.getLastKnownLocation(LocationManager.GPS_PROVIDER)

随意使用我的简单课程,它的作品就像一个魅力,你可以开发它的复杂的GPS项目

 public class GPS extends LocationListner{ Context mContext; boolean isGPSEnabled; boolean canGetLocation; boolean isPassiveEnabled; boolean isNetworkEnabled; Location location; LocationManager locationManager; public GPS(Context c) { this.mContext = c; } public Location getLocation() { int MIN_TIME_BW_UPDATES = 10000; int MIN_DISTANCE_CHANGE_FOR_UPDATES = 10000; if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(mContext, "PLZ open up GPS To Access", Toast.LENGTH_SHORT).show(); return null; } try { locationManager = (LocationManager) getApplicationContext() .getSystemService(LOCATION_SERVICE); isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); isPassiveEnabled = locationManager .isProviderEnabled(LocationManager.PASSIVE_PROVIDER); isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (isGPSEnabled || isNetworkEnabled || isPassiveEnabled) { this.canGetLocation = true; // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("GPS", "GPS Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); } } if (isPassiveEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.PASSIVE_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); } } if (isNetworkEnabled && location == null) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); Log.d("Network", "Network Enabled"); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } } else { return null; } } catch (Exception e) { e.printStackTrace(); } return location; } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } 

}

用你的行代替

 Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);