2014-07-26 23 views
0

我使用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() { 

} 
+0

有WiFi的工作與內部更好,那裏的GPS工作正常,在室外...同時檢查條件... –

回答

0

我解決這個問題。 LocationClient.getLastLocation()返回上次緩存的位置。我必須在LocationListener.onLocationChanged()中檢查位置,如果位置與上一個緩存的位置不相等,我可以使用它。

這裏是代碼:

import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.Toast; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 

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 = 1; 
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_HIGH_ACCURACY); 
    mLocationRequest.setInterval(UPDATE_INTERVAL); 
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 

    mLocationClient.connect(); 

} 

private final class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     // Report to the UI that the location was updated 

     // sendLocation(location); 

     if (!(location.getLatitude() == mCurrentLocation.getLatitude() && location 
       .getLongitude() == mCurrentLocation.getLongitude())) { 

      mLocationClient.removeLocationUpdates(locationListener); 
      mLocationClient.disconnect(); 

      //use location as answer 

     } 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 
    Toast.makeText(context, "Connection Failed", Toast.LENGTH_LONG).show(); 

} 

@Override 
public void onConnected(Bundle connectionHint) { 

    final LocationManager manager = (LocationManager) context 
      .getSystemService(Context.LOCATION_SERVICE); 

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 

     //GPS provider is not enabled 
     return; 
    } 

    locationListener = new MyLocationListener(); 
    mLocationClient.requestLocationUpdates(mLocationRequest, 
      locationListener); 
    mCurrentLocation = mLocationClient.getLastLocation(); 
    handler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      if (mLocationClient.isConnected()) { 
       mLocationClient.removeLocationUpdates(locationListener); 
       mLocationClient.disconnect(); 

       //GPS can not find the location 

      } 
     } 
    }, 2 * 60 * 1000); 


} 


@Override 
public void onDisconnected() { 

} 

}

0

在你的代碼

if(isInternetEnabled==true)//check for internet connection here 
{ 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
     locationListener); 
} 
else 
{ 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 
     locationListener); 
} 

嘗試這種方式,因爲它與我碰巧GPS_PROVIDER不給位置更新快速響應然後NETWORK_PROVIDER

Fused Location provider example它可以解決你的問題試試吧....

+0

我使用了LocationManager,但沒有奏效。關於Fused Location提供商,您是否看到我發佈的代碼?我現在使用Fused Location提供程序,但它不起作用! – Leo

+0

我已經提供了可以正常工作的鏈接,每次都可以獲取位置.... –