新しいAPIを使用して場所を取得しています。しかし、この新しい API を使用しても、デバイスの場所が古くなります。このメソッド (getLastLocation()) は、古い場所を取得しています。Wi-Fi が正常にオンになっている場合、実際の場所が取得されます。しかし、Wi-Fiがオフになると、古い場所が取得されます. Google マップのアプリはどのようにして位置情報をすばやく取得できるのでしょうか?
これは私が使用しているクラスです:
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;
public int res = 0;
boolean connected;
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);
sp = context.getSharedPreferences("GPS", 0);
editor = sp.edit();
}
@Override
public void onLocationChanged(Location location) {
if (connected && location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.i("NewGPSTracker", "Google Play Services Conectado.");
lc.requestLocationUpdates(lr, this);
connected = true;
location = lc.getLastLocation();
if (connected && location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("Location", ""+latitude + " "+longitude);
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.e("NewGPSTracker", ""+arg0);
}
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
}
}