LocationManager延迟获取GPS数据

时间:2017-11-06 15:46:13

标签: android android-studio android-gps

当我收到GPS数据时,我的手机有延迟问题。当用户按下需要用户位置的某些字段来验证数据时,将获取坐标。 Motorola手机运行正常。使用OnePlus 3T(A3000)和LG手机进行测试时,无论我选择的提供商和用户位置(车库,家庭,办公室),获得手机GPS的延迟大约需要12到15秒。 无论用户多少次回答相同的领域。

我已阅读链接:

Getting too much delay from OnLocationChange listener.?

Why Android LocationManager has long delay before location updates start if setting accuracy

Location Strategies

代码下方:

public void getLocation(final LocationListener locationListener) {
        if (locationListener == null) return;
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(500);
        locationRequest.setFastestInterval(500);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest)
                .setAlwaysShow(true);

        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        googleApiClient.connect();
        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
                .checkLocationSettings(googleApiClient, builder.build());

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

            @Override
            public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                criteria.setSpeedRequired(true);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setCostAllowed(true);
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        LocationManager locationManager =
                                ((LocationManager) getSystemService(Context.LOCATION_SERVICE));

                        showError("NETWORK: "+ locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
                        showError("GPS: "+locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));

                        String provider = locationManager.getBestProvider(criteria, true);

                        if (App.isVersionGreaterThanLollipop()){
                            if (ActivityCompat.checkSelfPermission(
                                    ChecklistActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                                    == PackageManager.PERMISSION_GRANTED) {
                                Location loc = locationManager.getLastKnownLocation(provider);
                                currentLcoation = loc;
                                showError("BEST PROVIDER: "+provider+" CURRENT LOCATION: " +loc.getLatitude()+" "+loc.getLongitude());
                                locationManager.requestSingleUpdate(
                                        criteria, locationListener, null);
                            }
                            else {
                                requestLocationPermission(locationListener);
                            }
                            ...
                        }
    ...

无论我是否强迫NETWORD_PROVIDER获得快速定位,它都会继续延迟。

如何让GPS数据过度延迟?

0 个答案:

没有答案