[英]How enforce Android to re-read GPS position?
I try to write application, which shows GPS coordinates on button click. 我尝试编写应用程序,该应用程序在单击按钮时显示GPS坐标。
@Override
public void onClick(View arg0) {
EditText editText;
editText = (EditText) findViewById(R.id.editText1);
LocationManager locationManager;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation("gps");
editText.setText(location.toString());
}
Is problem: while in first time I got coordinates of location with time of GPS reading, second click get the same location. 问题:第一次使用GPS读取时间获得位置坐标时,第二次单击获得相同位置。 Even when I stop program and run again, it get the same location! 即使我停止程序并再次运行,它也会获得相同的位置! How enforce to refresh reading GPS? 如何强制刷新阅读GPS?
while in first time I got coordinates of location with time of GPS reading, second click get the same location 第一次我得到的位置坐标与GPS读取时间有关,第二次单击则获得相同的位置
That is not surprising. 这不足为奇。
Even when I stop program and run again, it get the same location! 即使我停止程序并再次运行,它也会获得相同的位置!
That too is not surprising. 这也不足为奇。
How enforce to refresh reading GPS? 如何强制刷新阅读GPS?
Generally speaking, you don't. 一般来说,您不需要。 There is no requirement that the device has to have a fresh update just because you request the last-known location. 不需要仅因为您请求最后一个已知位置就必须对设备进行全新更新。 It may be physically impossible to provide a new location fix. 提供新的位置定位信息在物理上可能是不可能的。 And, if you are not calling requestLocationUpdates()
, it is very possible for getLastKnownLocation()
to simply return null
. 而且,如果您不调用requestLocationUpdates()
,则getLastKnownLocation()
很可能仅返回null
。
Your parameters to requestLocationUpdates()
may have some impact for how aggressively the device tries to update GPS, versus saving power. 您请求requestLocationUpdates()
参数可能会影响设备尝试更新GPS的积极程度,而不是节省电量。 However, there are no guarantees. 但是,没有任何保证。
Try to get best provider that available to fetch the Location data , using this code before you get the location 在获取位置之前,请尝试使用此代码来获取可用于获取位置数据的最佳提供商。
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
and in your getLastKnownLocation
pass the best Location Provider to get the location like this 并在您的getLastKnownLocation
传递最佳的位置提供程序来获取这样的位置
Location location = locationManager.getLastKnownLocation(bestProvider );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.