Android 定位失败
Android Location Failing
在 Android 中跟踪设备的纬度/经度的最佳方法是什么。我试过很多方法,没有一个有效。
我的设备是 运行 Android 4.1.2.
在我当前的代码中,有时 GPS 可以很好地工作,但有时它不起作用(给我纬度 0 和经度 0)。
这是我当前的代码:
locationManager = mLocationManager;
locationListener = new LocationListener() {
/**
* Fired when the location from the sensor changes.
*
* @param location Location object.
*/
@Override
public void onLocationChanged(Location location) {
sLatitude = String.valueOf(location.getLatitude());
sLongitude = String.valueOf(location.getLongitude());
sLatLon = sLatitude.concat(",").concat(sLongitude);
Log.d("TOMTOM LOCATION", location.toString());
}
/**
* Fired when the provider's status changes.
*
* @param provider The provider that has changed status.
* @param status The new status of the provider.
* @param extras Any extra data.
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
// Do nothing.
}
/**
* Fired when the provider has been enabled.
*
* @param provider Provider that has been enabled.
*/
@Override
public void onProviderEnabled(String provider) {
// Do nothing.
}
/**
* Fired when the provider has been disabled.
*
* @param provider Provider that has been disabled.
*/
@Override
public void onProviderDisabled(String provider) {
// Do nothing.
}
};
Log.d("TOMTOM", locationManager.getProviders(true).toString());
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("TOMTOM", "GPS enabled");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("TOMTOM", "Request GPS updates");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Request network updates");
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
} else {
Log.d("TOMTOM", "GPS disabled");
Log.d("TOMTOM", "Request network updates");
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
知道为什么我有时会收到 GPS 更新(记录为 Request GPS updates
)而有时会完全失败(记录为 Fail
)吗?
自从我在 Android 上做 GPS 工作已经一年左右了,如果我不完全正确,请多多包涵。也许有人可以纠正我。
GPS 系统不是实时的。您不能启动您的应用程序并期望立即获得位置。我相信你看到日志告诉你它是 Fail
的原因仅仅是因为 Android 并不总是有最后一个已知位置。因此,您可能会遇到 GPS 不可用(例如在建筑物中)的情况,您也没有有意义的最后已知位置。在这种情况下,它将只是空值。
您可以将读数之间的最短时间设置得非常小,甚至为零:
// The minimum distance to change Updates in meters
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
// The minimum time between updates in milliseconds
final long MIN_TIME_BW_UPDATES = 0; // 0 minute
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 && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
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();
}
}
}
在 Android 中跟踪设备的纬度/经度的最佳方法是什么。我试过很多方法,没有一个有效。
我的设备是 运行 Android 4.1.2.
在我当前的代码中,有时 GPS 可以很好地工作,但有时它不起作用(给我纬度 0 和经度 0)。
这是我当前的代码:
locationManager = mLocationManager;
locationListener = new LocationListener() {
/**
* Fired when the location from the sensor changes.
*
* @param location Location object.
*/
@Override
public void onLocationChanged(Location location) {
sLatitude = String.valueOf(location.getLatitude());
sLongitude = String.valueOf(location.getLongitude());
sLatLon = sLatitude.concat(",").concat(sLongitude);
Log.d("TOMTOM LOCATION", location.toString());
}
/**
* Fired when the provider's status changes.
*
* @param provider The provider that has changed status.
* @param status The new status of the provider.
* @param extras Any extra data.
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
// Do nothing.
}
/**
* Fired when the provider has been enabled.
*
* @param provider Provider that has been enabled.
*/
@Override
public void onProviderEnabled(String provider) {
// Do nothing.
}
/**
* Fired when the provider has been disabled.
*
* @param provider Provider that has been disabled.
*/
@Override
public void onProviderDisabled(String provider) {
// Do nothing.
}
};
Log.d("TOMTOM", locationManager.getProviders(true).toString());
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("TOMTOM", "GPS enabled");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("TOMTOM", "Request GPS updates");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Request network updates");
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
} else {
Log.d("TOMTOM", "GPS disabled");
Log.d("TOMTOM", "Request network updates");
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
知道为什么我有时会收到 GPS 更新(记录为 Request GPS updates
)而有时会完全失败(记录为 Fail
)吗?
自从我在 Android 上做 GPS 工作已经一年左右了,如果我不完全正确,请多多包涵。也许有人可以纠正我。
GPS 系统不是实时的。您不能启动您的应用程序并期望立即获得位置。我相信你看到日志告诉你它是 Fail
的原因仅仅是因为 Android 并不总是有最后一个已知位置。因此,您可能会遇到 GPS 不可用(例如在建筑物中)的情况,您也没有有意义的最后已知位置。在这种情况下,它将只是空值。
您可以将读数之间的最短时间设置得非常小,甚至为零:
// The minimum distance to change Updates in meters
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 0 meters
// The minimum time between updates in milliseconds
final long MIN_TIME_BW_UPDATES = 0; // 0 minute
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 && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
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();
}
}
}