Android GPS位置不正确

时间:2018-11-29 07:44:43

标签: android android-location android-gps android-fusedlocation

我正在开发的Android应用程序出现问题。该应用程序运行一个倒数计时器,并在计时器达到零时发送文本消息。在9秒钟时,该应用程序应该可以获取手机的位置。当计时器达到零时,它将在短信中作为地图链接发送位置(在9秒时收到)。

该应用程序可以运行,但是它发送的GPS位置有时在100米之外。我需要应用程序尽可能精确。我有一个使用android.location框架的版本和一个使用fusedlocationapi的版本。我拥有的android.location版本比fusedlocationapi准确得多,这对我来说很混乱。

真正困扰我的是:如果我运行计时器并获得不正确的位置,我可以打开google maps(它将显示一个非常准确的位置),回到我的应用程序,再次运行计时器,然后我会收到一个精确的位置(类似于Google地图刚刚向我显示的位置)。谷歌地图使用更好的方法来获取我的位置,还是我的代码不好?我以为该应用程序将显示与Google地图相同的位置,但事实并非如此,这让我非常沮丧。任何对此的想法将不胜感激。

这是我的代码,我在其中添加了fusionlocationapi:

  /**
     * Creating google api client object
     * */

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();

        mGoogleApiClient.connect();

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);

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

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult locationSettingsResult) {

                final Status status = locationSettingsResult.getStatus();

                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can initialize location requests here
                        getLocation();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(SafetyAlarmActivity.this, REQUEST_CHECK_SETTINGS);

                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        break;
                }
            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (requestCode) {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        // All required changes were successfully made
                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:
                        // The user was asked to change settings, but chose not to
                        break;
                    default:
                        break;
                }
                break;
        }
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.e(TAG, " mGoogleApiClient Connection connected:");

        getLocation();

    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        Log.e(TAG, "Connection failed: ConnectionResult.getErrorCode() = "+ connectionResult.getErrorCode());


    }

以下是应用在9秒内获取位置信息的代码:

    /*
    update GPS location
     */

    private void updateGPSLocation(long millisUntilFinished) {

        Log.e("current time remaining", " time: "+millisUntilFinished/1000);


        if(millisUntilFinished/1000==9)
        {

            Log.e("9 seconds remaining", "get locaiton");

            getLocation();
        }
    }

     /*
  get location
   */

    private void getLocation()
    {

        try
        {
            Location lastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);

            PersistObject.apply(con,AppConstant.LOCATION,lastLocation);

        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }


    }

这是我的权限:

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

0 个答案:

没有答案
相关问题