我尝试获取GPS数据。但GPS数据返回空值。当我看,下面的'位置'返回null,所以我无法获得GPS数据。
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);从Android手机获取GPS数据
我的代码如下;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled)
{
}
else if (!isGPSEnabled && !isNetworkEnabled)
{
}
else
{
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
我等你的帮忙。谢谢
我发现有时函数getLastKnownLocation()可以返回null,如果用户还没有激活/使用位置源一段时间。最好像你一样检查。如果它为空,则必须等待下一个位置更新 – chopchop 2013-03-27 22:39:38
或者设置requestSingleUpdate或requestLocationUpdates以自行打开GPS。 – 2013-03-27 22:52:45
如果提供者当前被禁用,则返回null。我使用GPS_PROVIDER。我如何启用? – efrahimates 2013-03-27 23:11:07