我正在使用GPS服务来获取纬度和频率在我的应用程序中的经度。当信号可用时,我能够获得纬度和经度。一旦我得到修复,如果我必须去GPS无法获得位置修复的地方,它仍然显示最后的位置。我想得到当前的位置而不是其他。我已经尝试使用onLocationChanged()和onGpsStatusChanged()两者来获取GPS状态,但它没有给我结果。
@Override
public void onGpsStatusChanged(int arg0) {
// TODO Auto-generated method stub
if (event == GpsStatus.GPS_EVENT_STARTED) {
Log.d("zmenaGPS" , "GPS event started ");
GpsStatus gs = mlocManager.getGpsStatus(null);
Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();
} else if (event == GpsStatus.GPS_EVENT_STOPPED) {
Log.d("zmenaGPS" , "GPS event stopped ");
GpsStatus gs = mlocManager.getGpsStatus(null);
Log.d("zmenaGPSgps status" , String.valueOf(gs));
Toast.makeText(getApplicationContext(),String.valueOf(gs),Toast.LENGTH_LONG).show();
} else if (event == GpsStatus.GPS_EVENT_FIRST_FIX) {
Log.d("zmenaGPS" , "GPS fixace ");
Toast.makeText(getApplicationContext(),"First Fix",Toast.LENGTH_LONG).show();
} else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
Log.d("zmenaGPS" , "GPS EVET NECO ");
}
}
答案 0 :(得分:1)
使用NETWORK_PROVIDER来获取所有时间的更新位置。 喜欢这个..
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(Context.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;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
// locationManager.requestLocationUpdates(
// LocationManager.NETWORK_PROVIDER,
// MIN_TIME_BW_UPDATES,
// MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
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, 0, 0, locationListenerGps);
// 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();
}
}
}
}
}
// if (!isGPSEnabled) {
// showSettingsAlert();
// }
} catch (Exception e){
e.printStackTrace();
}
return location;
}
希望这有帮助
答案 1 :(得分:1)
听起来您想要获取当前位置,并且您想知道当前位置何时未知,因此您不要尝试使用旧位置。在这种情况下,您可以尝试获取当前位置,然后测试它是否旧。
long fresh=60000; // 1 minute in milliseconds
if(System.currentTimeMillis()-location.getTime()>fresh)
// Do something useful with location
答案 2 :(得分:-1)
如果您无法使用GPS获取该位置,则可能需要使用NETWORK PROVIDER
获取该位置,并与上次位置修复进行比较,以了解哪一个更好并相应地使用。