首页 文章

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

提问于
浏览
0

我现在有一项服务 . 它会检查GPS是否已启用,如果是GPS,它将获得GPS位置,我的 Map 可以缩放到该位置 . 它有 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, Map 上的蓝点更新 . 它不会放大它 . 由于GPS已经启用(但无法修复),它沿着那条路走下去,然后又回到了我的房子里 LastKnownLocation() . 即使蓝点是正确的,它仍然缩放到我最后的位置 .

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

希望有人可以帮助我 . 我不像从代码中删除 lastKnownLocation 那么简单 .

感谢您提供的任何帮助 .

1 回答

  • 1

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

    • 不要使用旧的LastKnownLocation . 首先检查年龄!

    • Location通过 getProvider() 提供提供商,检查您是否具有最高优先级,然后在辅助服务器上工作 .

    • 使用具有最高精度的GPS .

    您可能需要仔细查看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);
    }
    

相关问题