11

我正在使用 Fused location Api,我得到的位置精度约为 10 米。有时它可以提供 4 到 8 米的精度。我希望使用此融合位置 Api 或任何其他方式获得更高的准确性。有没有办法以低于 4 米的精度获得位置。

我通过这种方式获得位置。

    public class GetCurrentLocation implements
            ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

        private static final String TAG = "location-updates-sample";
        public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 0;
        public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
                UPDATE_INTERVAL_IN_MILLISECONDS / 2;
        private final String REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key";
        private final String LOCATION_KEY = "location-key";
        private final String LAST_UPDATED_TIME_STRING_KEY = "last-updated-time-string-key";
        private GoogleApiClient mGoogleApiClient;
        private LocationRequest mLocationRequest;

        private Context mContext;
        private getLocation mGetCurrentLocation;

        public GetCurrentLocation(Context context) {
            mContext = context;

            buildGoogleApiClient();
        }

        private synchronized void buildGoogleApiClient() {
            Log.i(TAG, "Building GoogleApiClient");
            mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            createLocationRequest();
        }

        public interface getLocation{
            public void onLocationChanged(Location location);
        }

        public void startGettingLocation(getLocation location) {
            mGetCurrentLocation = location;
            connect();
        }

        public void stopGettingLocation() {
            stopLocationUpdates();
            disconnect();
        }

        private void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
            mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }

        private void startLocationUpdates() {
            if (mGoogleApiClient.isConnected()) {
                LocationServices.FusedLocationApi.requestLocationUpdates(
                        mGoogleApiClient, mLocationRequest, this);
            }
        }
    private void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }



    private void connect() {
        mGoogleApiClient.connect();
    }

    private void disconnect() {
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "Connected to GoogleApiClient");
        startLocationUpdates();

    }

    @Override
    public void onLocationChanged(Location location) {
        mGetCurrentLocation.onLocationChanged(location);
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
    }
}

我在我的活动代码中使用这个类。

private GetCurrentLocation mListen;
mListen = new GetCurrentLocation(this);
            mListen.startGettingLocation(new GetCurrentLocation.getLocation() {
                @Override
                public void onLocationChanged(Location location) {
                   // Here is my working with location object
                }

            });

我如何优化此代码以获得准确的位置。谢谢。

4

2 回答 2

8

位置准确性基于 GPS/网络提供商,您只能等待并从他们那里获取位置。

您可以再次确定准确性

@Override
public void onLocationChanged(Location location) {
   int suitableMeter = 20; // adjust your need
   if (location.hasAccuracy()  && location.getAccuracy() <= suitableMeter) {
        // This is your most accurate location.
   }

}
于 2015-05-28T03:43:41.677 回答
8

2020年最佳官方解决方案

Google API Client/FusedLocationApi 已被弃用,位置管理器根本没有用处。 所以 Google 更喜欢使用 Google Play 服务位置 API 的 Fused Location Provider “FusedLocationProviderClient”用于获取位置及其更好的省电和准确性方法

这是 kotlin 中的示例代码,用于获取最后一个已知位置/一次性位置(相当于当前位置)

 // declare a global variable of FusedLocationProviderClient
    private lateinit var fusedLocationClient: FusedLocationProviderClient

// in onCreate() initialize FusedLocationProviderClient
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)

 /**
     * call this method for receive location
     * get location and give callback when successfully retrieve
     * function itself check location permission before access related methods
     *
     */
    fun getLastKnownLocation() {
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location->
                    if (location != null) {
                       // use your location object
                        // get latitude , longitude and other info from this
                    }

                }

    }

如果您的应用可以持续跟踪位置,那么您必须接收接收位置更新

检查 kotlin 中的示例

// declare a global variable FusedLocationProviderClient
        private lateinit var fusedLocationClient: FusedLocationProviderClient
    
    // in onCreate() initialize FusedLocationProviderClient
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)
    

      // globally declare LocationRequest
        private lateinit var locationRequest: LocationRequest
    
        // globally declare LocationCallback    
        private lateinit var locationCallback: LocationCallback
    
    
        /**
         * call this method in onCreate
         * onLocationResult call when location is changed 
         */
        private fun getLocationUpdates()
        {
    
                fusedLocationClient = LocationServices.getFusedLocationProviderClient(context!!)
                locationRequest = LocationRequest()
                locationRequest.interval = 50000
                locationRequest.fastestInterval = 50000
                locationRequest.smallestDisplacement = 170f // 170 m = 0.1 mile
                locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY //set according to your app function
                locationCallback = object : LocationCallback() {
                    override fun onLocationResult(locationResult: LocationResult?) {
                        locationResult ?: return
    
                        if (locationResult.locations.isNotEmpty()) {
                            // latest location is on 0th index
                            val location =
                                LatLng(locationResult.locations[0].latitude, locationResult.locations[0].longitude)
                            // use your location object
                            // get latitude , longitude and other info from this
                        }
    
    
                    }
                }
        }
    
        //start location updates
        private fun startLocationUpdates() {
            fusedLocationClient.requestLocationUpdates(
                locationRequest,
                locationCallback,
                null /* Looper */
            )
        }
    
        // stop location updates
        private fun stopLocationUpdates() {
            fusedLocationClient.removeLocationUpdates(locationCallback)
        }
    
        // stop receiving location update when activity not visible/foreground
        override fun onPause() {
            super.onPause()
            stopLocationUpdates()
        }
    
        // start receiving location update when activity  visible/foreground
        override fun onResume() {
            super.onResume()
            startLocationUpdates()
        }

确保您注意位置的 Mainfaist 权限和运行时权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

并为 Gradle 添加这个

implementation 'com.google.android.gms:play-services-location:17.0.0'

有关更多详细信息,请遵循这些官方文档

https://developer.android.com/training/location/retrieve-current

https://developer.android.com/training/location/receive-location-updates

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient

于 2019-08-12T19:50:45.630 回答