我使用LocationClient獲取位置。通過WiFi連接,它可以正常工作,但沒有WiFi,它會重複相同的位置(即使使用移動數據連接!)。我使用了LocationManager,但沒有奏效。android - 重複相同的位置沒有WiFi連接
我想知道如何谷歌地圖工作正常(移動數據連接),但我無法得到正確的位置。
使用「LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY」或「LocationRequest.PRIORITY_HIGH_ACCURACY」得到相同的結果。
這裏是我的代碼:
public class LocationTracker implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
LocationClient mLocationClient;
Location mCurrentLocation;
LocationRequest mLocationRequest;
LocationListener locationListener;
Handler handler = new Handler();
private static final int MILLISECONDS_PER_SECOND = 1000;
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
private static final int FASTEST_INTERVAL_IN_SECONDS = 5;
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;
Context context;
public LocationTracker(Context context) {
this.context = context;
}
public void getLocation() throws Exception {
try {
final int result = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (result != ConnectionResult.SUCCESS) {
Toast.makeText(
context,
"Google Play service is not available (status="
+ result + ")", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
throw new Exception(e);
}
mLocationClient = new LocationClient(context, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationClient.connect();
}
private final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Toast.makeText(context, "Connection Failed", Toast.LENGTH_LONG).show();
}
@Override
public void onConnected(Bundle connectionHint) {
locationListener = new MyLocationListener();
mLocationClient.requestLocationUpdates(mLocationRequest,
locationListener);
mCurrentLocation = mLocationClient.getLastLocation();
}
@Override
public void onDisconnected() {
}
有WiFi的工作與內部更好,那裏的GPS工作正常,在室外...同時檢查條件... –