如何仅根据最低位置精度来更新位置

时间:2019-06-13 18:25:11

标签: android location android-gps fusedlocationproviderclient

我目前正在开发需要高精度位置更新的位置跟踪应用。 这是我的第一个应用程序,可能会困扰我,但我正在学习。 我在使用location.distanceTo()时从熔融的LocationProviderClient获得的精度是每次更新(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 个答案:

答案 0 :(得分:0)

尝试

  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));
        }
    }