gpt4 book ai didi

Android 位置有时太旧

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:33 25 4
gpt4 key购买 nike

当我试图在 android 中获取设备位置时,我遇到了一个奇怪的问题。

有时它能很好地获取当前位置,但有时它会不断返回旧位置。我所说的旧指的是距离它应该位于的地方几十甚至几百公里。

我不知道我做错了什么,我尝试了几种不同的方法来获取位置,但似乎每种方法都有同样的问题。

最奇怪的是它并不是在每台设备上都会发生……或者至少看起来是这样。三星 S3 和 S4 受影响最大,但在我的 nexus 4 中从未发生过。

这是我的代码,也许你看错了:

    public void startSearchingLocation() {

// Define a listener that responds to location updates
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.


makeUseOfNewLocation(location);

}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}

};

String provider = getProvider();

/*I ignore the passive provider*/
if (provider == null
|| provider.contains(LocationManager.PASSIVE_PROVIDER)) {
this.validProvider = false;
} else {
this.validProvider = true;
}
if (validProvider) {
locationManager.requestLocationUpdates(provider, time, distance,
locationListener);

}
}

protected void makeUseOfNewLocation(Location location) {
JSONObject obj = new JSONObject();

try {
obj.put("latitude", location.getLatitude());
obj.put("longitude", location.getLongitude());
obj.put("provider", location.getProvider());
locationResult = obj;
} catch (Exception e) {
e.printStackTrace();
}
}

public JSONObject getLastKnownLocation() {
if (locationResult != null) {
return locationResult;
} else {
String provider = getProvider();
Location location = locationManager.getLastKnownLocation(provider);

if (location != null) {
JSONObject obj = new JSONObject();

try {
obj.put("latitude", location.getLatitude());
obj.put("longitude", location.getLongitude());
obj.put("provider", location.getProvider());
return obj;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}

public String getProvider() {
Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);

String provider = locationManager.getBestProvider(criteria, true);

return provider;

}

谢谢

注意:这不是互联网连接不良。一些用户已经开始提示这个问题。而其他使用地理定位的应用程序工作正常。它一定是我的代码中的东西,但我不知道是什么。最奇怪的是,它似乎在一两周内运行良好,然后它开始同时在很多用户中发生

最佳答案

我会说问题出在

criteria.setPowerRequirement(Criteria.POWER_LOW);

这可能只是返回被动位置提供者。我会删除它,用户现在知道这种应用程序会消耗电池,因此它不会成为强制标准。

无论如何,我有一个更复杂的类来检索位置。让我解释一下。

首先,from here我使用这个很棒的功能来确定一个位置读数是否比当前位置更好。

 /** 
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new
* one
*/
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);
}

然后,我创建自己的类来实现 LocationListener( Activity 或辅助类,由您决定)。我只考虑 gps 和网络提供商,但它很容易扩展

public class LocationObserver implements LocationListener {

LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false, network_connected = false;


Context context;
Long timeout;
int minTime;
int minDistance;
Handler handler = new Handler();
private Location currentLocation = null;

public LocationObserver(Context pContext, int minTime,
int minDistance, long timeout) {

this.context = pContext;
this.timeout = timeout;
this.minDistance = minDistance;
this.minTime = minTime;
}

我创建了一个方法开始,我考虑哪个是最好的提供者,或者我是否可以听多个

    public void start() {
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);

// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
//dont worry yet
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
network_connected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
} catch (Exception ex) {
//dont wory yet
}

//now, lets see what happend,

//don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled) {

//nothing enabled, alert the user to enable any provide
//Settings.ACTION_LOCATION_SOURCE_SETTINGS;



}

else { // one provider is enabled, and the other one is not


if (gps_enabled) { // gps enabled, network disabled
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, this);

//Consider asking the user to also enable network location ( Settings.ACTION_LOCATION_SOURCE_SETTINGS;=


} else { // gps disabled, network enabled


// check if actually there is a conection
if (network_connected) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
minTime, minDistance, this);

//OK, Network is working,
//consider asking the user to enable also gps (Settings.ACTION_LOCATION_SOURCE_SETTINGS;)

}

//if network is not enabled, having the network location enabled is useles

else {

//ask the user to connect to any network ( Settings.ACTION_WIRELESS_SETTINGS)
}

}
}


//retrieve the lastKnownLocation (we will see this function later) and call the callback to start using it
handler.postDelayed(new Runnable() {
@Override
public void run() {

Location l = getLastKnownLocation();
onLocationChanged(l);

}
}, timeout);

}

我们制作了自己的 getLastKnownLocation(),它考虑了所有启用的提供者,并返回最佳位置

    public Location getLastKnownLocation() {

Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// if there are both values use the best one
if (gps_loc != null && net_loc != null) {
if (isBetterLocation(gps_loc, net_loc))
return gps_loc;
else
return net_loc;

}

else if (gps_loc != null) {
return gps_loc;

} else if (net_loc != null) {
return net_loc;

} else
return null;
}

最后,我们编写 onLocationChanged 回调,提供者将调用任何更新。由于所有供应商都会调用它,可能即将到来的位置比我们已经拥有的位置差,所以我们也将它们进行比较

public synchronized void onLocationChanged(Location location) {

if (location == null || currentLocation == null
|| isBetterLocation(location, currentLocation)) {
currentLocation = location;


//do whatever you need with the new location

}
}

}

另外,我过去常常用计时器检查位置是否在第一次调用 getlastknowlocation 之后出现。如果特定时间过去了,我会再次检查所有内容,以查看启用了哪些提供程序,或者再次要求用户启用已禁用的提供程序。

此外,有些人喜欢直接依赖 Play Services Location Apis,您不必担心提供商,也无需关注底层定位技术的细节。 https://developer.android.com/google/play-services/location.html

关于Android 位置有时太旧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20969401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com