通过GPS然后网络然后Wifi获取用户的位置

时间:2014-03-05 13:51:28

标签: android networking geolocation gps location

我现在有一项服务。它会检查GPS是否已启用,如果是GPS,它将获得GPS位置,我的地图可以缩放到该位置。它有getLastKnownLocation。问题是,getLastKnownLocation可能在几英里之外(就像我昨天尝试时那样)。

它首先运行GPS检查,因为它启用它不会运行网络检查位置。

有没有办法让它如果启用GPS,但是除了getLastKnownLcation()之外无法获得修复将默认为基于网络的位置?之后我会检查,如果网络没有启用或者lastKnownLocation太远,我可以检查Wifi。

以下是我的服务代码:

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;
double latitude;
double longitude;

//Minimum distance for update
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 40; //40 seconds

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    Log.i("i", "Get location called");

    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    //Getting GPS status
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    //Getting network status
    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    this.canGetLocation = true;
    if (isGPSEnabled) {
        if (location == null) {
            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();
                    Log.i("GPS_LOCATION", "Location: "+location.toString());
                    if(location.toString().contains("0.000000")) {
                        Log.i("Called", "Called inside");
                        isNetworkEnabled = true;
                        isGPSEnabled = false;
                        getLocation();
                    }
                }
            }
        } 
    }
    else if (isNetworkEnabled) {
        Log.d("Network", "Network");
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                Log.i("NETWORK_LOCATION", "Location: "+location.toString());
            }
        }
    }
    else if (!isGPSEnabled && !isNetworkEnabled) {
        // no network provider is enabled and GPS is off.
        Log.d("NOT ENABLED", "Use WIFI");
    }
    return location;
}

昨天发生的事情是我离家几英里远,有无线连接并启用了GPS,但发生了什么事,虽然我的位置通过Wifi,地图上的蓝点更新。它不会放大它。由于GPS已经启用(但无法修复),它沿着那条路走下去,并且LastKnownLocation()已经回到了我家。即使蓝点是正确的,它仍然缩放到我最后的位置。

我有没有办法让它检查GPS但不使用LastKnownLocation?它默认为网络检查,然后网络检查将不使用lastknownLocation,它将默认为Wifi。如果需要,Wifi可以有LastKnownLocation。实际上我只想在Wifi阶段获得lastKnownLocation作为最后的手段。

希望有人可以帮助我。我认为这不如从代码中删除lastKnownLocation那么简单。

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

有各种各样的方法可以做到这一点。这里有一些提示:

  1. 不要使用旧的LastKnownLocation。先检查年龄!
  2. 位置通过getProvider()向提供商提供,检查您是否拥有最高优先级,然后在辅助服务器上工作。
  3. 使用具有最高精度的GPS。
  4. 您可能需要仔细查看Location Strategies,特别是isBetterLocation函数。你可能想要这样的东西,虽然你需要调整它以满足你的需要。

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }
    
        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;
    
        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
        // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }
    
        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;
    
        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());
    
        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }
    
    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
          return provider2 == null;
        }
        return provider1.equals(provider2);
    }