Android 获取用户';通过GPS定位,然后通过网络定位,再通过Wifi定位
目前我有一项服务,这种服务很有效。它检查GPS是否启用,如果启用,它将获取GPS位置,并且我的地图可以缩放到该位置。它具有Android 获取用户';通过GPS定位,然后通过网络定位,再通过Wifi定位,android,networking,geolocation,gps,location,Android,Networking,Geolocation,Gps,Location,目前我有一项服务,这种服务很有效。它检查GPS是否启用,如果启用,它将获取GPS位置,并且我的地图可以缩放到该位置。它具有getLastKnownLocation。问题是,getLastKnownLocation可能在几英里之外(就像我昨天尝试的那样) 它首先运行GPS检查,因为它已启用,所以不会运行网络位置检查 是否有一种方法可以使它在启用GPS但无法获得除GetLastKnownLocation()之外的其他修复时默认为基于网络的位置?之后,我将进行检查,以便如果网络未启用或LastKnow
getLastKnownLocation
。问题是,getLastKnownLocation
可能在几英里之外(就像我昨天尝试的那样)
它首先运行GPS检查,因为它已启用,所以不会运行网络位置检查
是否有一种方法可以使它在启用GPS但无法获得除GetLastKnownLocation()之外的其他修复时默认为基于网络的位置?之后,我将进行检查,以便如果网络未启用或LastKnown位置太远,我可以检查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;
}
昨天发生的事情是,我离家几英里远,连接了wifi并启用了GPS,但发生的事情是,虽然我的位置通过wifi更新,但地图上的蓝点。它不会放大它。由于GPS被启用(但无法得到修复),它沿着这条路线运行,得到了回到我家的lastnownlocation()
。尽管蓝点是正确的,但它一直在缩放到我上次所在的位置
有没有办法让它检查GPS,但不使用lastnownlocation
?它默认为网络检查,然后网络检查将不使用lastnownlocation
,它将默认为Wifi。如果需要,Wifi可以具有LastKnown位置。实际上,我只想在Wifi阶段获得lastKnownLocation
,作为最后的手段
希望有人能在这方面帮助我。我认为这并不像从代码中删除lastnownlocation
那么简单
感谢您提供的任何帮助。有各种方法可以做到这一点。以下是一些提示:
getProvider()
提供提供程序,首先检查您是否具有最高优先级,然后再处理辅助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);
}
受保护的布尔值isBetterLocation(位置-位置,位置-当前最佳位置){
如果(currentBestLocation==null){
//新位置总比没有位置好
返回true;
}
//检查新的位置修复程序是较新的还是较旧的
long-timeDelta=location.getTime()-currentBestLocation.getTime();
布尔值isSignificantlyNewer=时间增量>两分钟;
布尔值Issignificantlolder=时间增量<-2_分钟;
布尔值isNewer=timeDelta>0;
//如果距离当前位置已超过两分钟,请使用新位置
//因为用户可能已经移动了
如果(非常重要){
返回true;
//如果新位置比原来的位置早两分钟以上,情况肯定会更糟
}else if(Issignificantlolder){
返回false;
}
//检查新的位置修复是否更精确
int accuracyDelta=(int)(location.getAccuracy()-currentBestLocation.getAccuracy());
布尔值IslesAccurate=accuracyDelta>0;
布尔值IsmorePrecision=精度偏差<0;
布尔值不太精确=精度偏差>200;
//检查新旧位置是否来自同一提供商
布尔值isFromSameProvider=isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
//结合及时性和准确性确定定位质量
如果(更准确){
返回true;
}else if(isNewer&!islesAccurate){
返回true;
}else if(较新且不太准确且来自SameProvider){
返回true;
}
返回false;
}
/**检查两个提供程序是否相同*/
专用布尔值isSameProvider(字符串提供程序1、字符串提供程序2){
if(provider1==null){
返回provider2==null;
}
返回provider1.equals(provider2);
}