如何仅根据最低位置精度更新位置
Posted
技术标签:
【中文标题】如何仅根据最低位置精度更新位置【英文标题】:How to only update location based on minimum location accuracy 【发布时间】:2019-10-28 09:42:52 【问题描述】:我目前正在开发一个需要高精度位置更新的位置跟踪应用。 这是我的第一个应用程序,我可能会不知所措,但我正在学习。 我在使用 location.distanceTo() 时从 fusedLocationProviderClient 获得的准确度在每次更新(5 秒间隔)之间移动 15 到 70+ 米,同时以恒定的速度行走。 有没有办法提高精度或设置最小精度?
我在文档中找到了 getAccuracy() 和 hasAccuracy() 方法,但到目前为止我还没有理解如何使用这些方法来消除波动的准确性。
编辑:我遇到了将条件添加到我的位置代码的选项。 您能否提供一个示例,说明如何将其合并到我的代码中以获得始终如一的高精度?
或者,将我的代码更改为专门使用 LocationManager 和 GPS 卫星会更好吗?因为我的所有会话都是在外面完成的?
@Override
public void onMapReady(GoogleMap googleMap)
mGoogleMap = googleMap;
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setMinZoomPreference(8.0f);
mGoogleMap.setMaxZoomPreference(25.0f);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(3000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
//Location Permission already granted
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
else
//Request Location Permission
checkLocationPermission();
else
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mGoogleMap.setMyLocationEnabled(true);
LocationCallback mLocationCallback = new LocationCallback()
@Override
public void onLocationResult(LocationResult locationResult)
float zoomLevel = mGoogleMap.getCameraPosition().zoom;
List<Location> locationList = locationResult.getLocations();
System.out.println(workoutLocationList);
if (locationList.size() > 0)
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Location lastLocation = location;
locationList.add(location);
workoutLocationList.add(location);
if(workoutLocationList.size() > 1)
lastLocation = workoutLocationList.get(workoutLocationList.size() - 2);
System.out.println(workoutLocationList.size());
for (Location i : workoutLocationList)
System.out.println(i);
System.out.println("current location is: "+ location);
System.out.println("previous location is: "+ lastLocation);
distanceBetweenLocations += location.distanceTo(lastLocation);
workoutTotalDistance+= distanceBetweenLocations;
System.out.println((int)distanceBetweenLocations);
Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude());
mLastLocation = location;
if (mCurrLocationMarker != null)
mCurrLocationMarker.remove();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
【问题讨论】:
【参考方案1】:试试这个
if (ActivityCompat.checkSelfPermission(DriverMap.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
DriverMap.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(DriverMap.this, new String[]android.Manifest.
permission.ACCESS_FINE_LOCATION, 1);
else
if (!mMap.isMyLocationEnabled())
mMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocation = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (myLocation == null)
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = lm.getBestProvider(criteria, true);
myLocation = lm.getLastKnownLocation(provider);
if (myLocation != null)
latitude = myLocation.getLatitude();
lonitude = myLocation.getLongitude();
userLocation = new LatLng(latitude, lonitude);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12), 1500, null);
pos = mMap.addMarker(new MarkerOptions().position(userLocation).title("ur loc"));
pos.setDraggable(true);
pos.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.car));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 17));
【讨论】:
所以根据您的回答,我认为我的概念无法使用 fusedLocationProviderClient?如果我切换到 LocationManager,是否还需要 LocationCallback 方法? 您可以同时使用 yes 或 OnLocationChanged ,以上是关于如何仅根据最低位置精度更新位置的主要内容,如果未能解决你的问题,请参考以下文章
在后台更新低准确度的地理围栏,以高精度更新地图上的位置,无法按预期工作