【发布时间】:2021-06-10 08:48:37
【问题描述】:
我试图通过单击按钮获得准确的 GPS 位置。我正在使用 fusesLocationProviderClient 并拥有 FINE_LOCATION 权限。
private void getCurrentGPSLocation() {
// get the new location from the fused client
// update the UI - i.e. set all properties in their associated text view items
//Initialize new location request
LocationRequest locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10000)
.setFastestInterval(1000)
.setNumUpdates(1);
//Initialize location call back
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
//Initialize location1
Location location1 = locationResult.getLastLocation();
//Set Accuracy
double accura1 = location1.getAccuracy();}
};
//Request location updates
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest
, locationCallback, Looper.myLooper());
}
然后我从 location1 变量中获取坐标,因为我从中获得了准确性。问题是这非常不精确,因为我得到的精度为 800-1000m。在打开 GoogleMaps 几秒钟后返回到我的应用程序调用 getLastLocation();使用另一个按钮(不是我的代码示例的一部分),我的精度为 4-5m。再次使用我的代码时,精度再次为 800-1000m。 所以我的问题是,如何更改我的代码以在我的 getCurrentGLSLpcation() 方法中获得这种准确性。
【问题讨论】:
-
去掉
.setNumUpdates(1),设置.setInterval(5000),.setFastestInterval(4000)然后等待onLocationResult5或10s,效果会更好。 -
感谢您的回复。我实际上被困在如何将 onLocationResult 的延迟放入我的代码中。我发现了这个:link 但我不知道如何将处理程序放入我的代码中。你能帮帮我吗?
-
new Handler().postDelayed(new Runnable() { @Override public void run() { //在这里调用你的函数。} }, mInterval);
标签: android android-gps