- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我试图在 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/
几周前,我安装了一个新的 ssl 证书来替换一个过期的证书。 .crt 和 .key 的文件名保持不变。只是内容变了。 现在,当我访问我的网站时,它说证书已过期,我看到它正在使用旧的证书链。我可以确认
这个问题在这里已经有了答案: Make namespaces backwards compatible in PHP (4 个回答) 8 年前关闭。 我真的很想在我的工作流程中采用命名空间。据我了解,
我从 CN1 开始,然后以 Todo App 为例。当我在 Netbeans 中运行 de app 时,只出现一个空白表单,我更改了主题,添加了一个 jpg 图像并使用旧的 GUI Builder 在
我想知道是否有一些 Laravel 人员可以帮忙。 我有一个表单,其中有 2 个单选按钮,当表单提交时,它会通过验证器,如果验证器失败,它会返回表单,使用输入填充字段并显示错误消息。 我似乎无法对单选
我正在使用 Java 中没有泛型的旧代码。 hashMap 是在该代码中定义的,没有泛型,例如: Map A = new HashMap(); 在这段代码中,我想如何确定要在键和值中放入什么内容,以及
我想问一个关于项目兼容性的问题。我的论文项目是在Windows Vista和XP上在JRE 1.6和JDK 1.6下开发的。该项目使用Java新套接字。今天我尝试在 Windows 8(64 位)机器
有谁知道一个脚本可以将旧的 Products 命名空间样式 Plone 附加组件包装到 Egg 中吗? 生成setup.py 创建目录结构 还需要采取其他措施吗? 鸡蛋化过程中有什么陷阱吗? 最佳答案
我已经运行 MySQL 5.6.12 一段时间了。我决定将我的 WAMP 服务器更新到最新的 PHP 版本。 在此过程中,它还将 MySQL 更新到 5.6.17,保留 .12 数据,但无法访问。 如
最近,我一直在尝试根据互联网上的各种旧教程编写论坛代码,但是我最近遇到了一个问题 - 尽管我完全按照教程所述进行操作,但我收到了空格错误。我认为这可能是因为某些 MySQL 命令可能已更改。如果有人可
我正在创建对 Count 表的查询。 $Month = $_POST['Month']; $query = "SELECT ANY_VALUE(AD) AS ad, COU
如果我使用 mysqldump 工具备份旧版本的 MySql 数据库,是否存在任何已知风险?例如,如果我在生产机器上使用 mysqldump 5.6 来备份 MySql 5.X 数据库。 最佳答案 有
当将 columnsData 值分配给 columns 时,我有两个 JSON 对象 columnsData 和 columns,这两个值都会更改。 var columnsData = [
我有一个需要在 gcc 4.4 上编译的多线程应用程序,我不允许使用 c++0x 标志。 我希望一个变量以原子方式运行,但不幸的是没有 C++0x 标志我无法使用 atomic在 C++ 中。 我试过
我可以借助广播事件(ACTION_TIME_CHANGED 和 ACTION_DATE_CHANGED)获取时间更改事件。 我需要在时间更改后获取之前的时间。例如,当前时间是 10:00。我要把时间改
我正在尝试在我的 Android 应用程序中创建一个 DatePickerDialog,但是当我创建一个 DatePickerDialog 时,我收到以下消息:Call requires API le
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!} {{ Form::labe
我的问题与 iOS 周围的蓝牙技术有关。我看过关于蓝牙低功耗 101、新功能、基础知识等的 WWDC,以及关于使用 iOS 5 及更高版本中可用的 CoreBluetooth 框架的内容。我浏览了不同
我有一个有五个屏幕的应用。 在每个屏幕上,我在 viewDidLoad 中从服务器访问数据。 在每个屏幕上我都有下一个按钮。 当我从屏幕一转到屏幕五(通过单击下一步 4 次)时,在 NSLog 中,我
我最近在一家网络报纸找到了一份工作。在网站上,我们有一个非常古老且重要的 Symfony 应用程序,它是为一位年长的开发人员编写的,已经消失很久了。该应用程序是神圣的:是报纸收入的血液。问题是我们没有
我相信我已经找到了一种方法来实现类似可移植 C89 中众所周知的“struct hack”的方法。我很好奇这是否真的严格符合 C89。 主要思想是:我分配足够大的内存来容纳初始结构和数组元素。确切的大
我是一名优秀的程序员,十分优秀!