我使用新的 API 来获取位置:android. https://developer.android.com/training/location/retrieve-current.html
有时当我得到位置时,它已经过时了。我在某个地方下车去另一个地方,然后应用程序将第一个位置返回给我。当我退出WiFi时会发生这种情况。当我使用 WiFi 时,它工作得很好。我是否缺少某种方法或其他东西?
public class NewGPSTracker implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener, com.google.android.gms.location.LocationListener {
private LocationRequest lr;
private LocationClient lc;
Location location;
double latitude = 0;
double longitude = 0;
Context context;
static Boolean status = false;
public int res = 0;
boolean conectado;
public NewGPSTracker(Context context) {
this.context = context;
lr = LocationRequest.create();
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lr.setInterval(5000);
lr.setFastestInterval(1000);
lc = new LocationClient(context, this, this);
}
@Override
public void onLocationChanged(Location location) {
if (conectado) {
lc.removeLocationUpdates(this);
lc.requestLocationUpdates(lr, this);
location = lc.getLastLocation();
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.e("NewGPSTracker", ""+arg0);
}
@Override
public void onConnected(Bundle connectionHint) {
conectado = true;
Log.i("NewGPSTracker", "Google Play Services Conectado.");
lc.requestLocationUpdates(lr, this);
location = lc.getLastLocation();
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public void connect(){
lc.connect();
}
public void disconnect(){
lc.disconnect();
}
@Override
public void onDisconnected() {
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}