【问题标题】:Get location instantly after activate GPS激活 GPS 后立即获取位置
【发布时间】:2016-10-04 10:32:49
【问题描述】:

当我启动我的活动时,我想使用他的 gps 获取用户的位置。 如果 GPS 未打开,我会举杯祝他打开他的 gps 以从特定功能中受益。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .enableAutoManage(this, 34992, this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 50,SelectCinemaActivity.this );
    // Define the criteria how to select the locatioin provider -> use default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);



    setContentView(R.layout.fragment1);

    requestLocation();
    locationChecker(mGoogleApiClient, Activity.this);

    ...
}

我的 requestLocation() 函数

    public void requestLocation(){

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        latitudeActuelle = location.getLatitude();
        longitudeActuelle = location.getLongitude();
        try {
            initList();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("Location not available");

        try {
            initListWithoutDistance();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

这里是带有 Toast 的 locationChecker:

public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    Log.i("SUCCESS", String.valueOf(status.getStatusCode()));
                    requestLocation();
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.

                    Log.i("RES REQUIRED", String.valueOf(status.getStatusCode()));
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                activity, 1000);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                    Log.i("UNAIVALEBLE", String.valueOf(status.getStatusCode()));
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    try {
                        initListWithoutDistance();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    Log.i("IL CLIQUE", "OK");
                    location = locationManager.getLastKnownLocation(provider);
                    Log.i("OK CLICK", String.valueOf(location));
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    Log.i("IL CLIQUE", "CANCEL");
                    try {
                        initListWithoutDistance();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
            break;
    }
}

initList 和 initListWithoutDistance 是 2 个按距离或名称对结果进行排序的函数。

当 GPS 处于活动状态时,我没有问题。 当 GPS 关闭时,我点击“确定”按钮,我无法立即获得当前位置。我试图通过调用 location = locationManager.getLastKnownLocation(provider); 来获取 onLocationChanged 事件,但结果为空。我无法直接调用 onLocationChanged 函数,因为我没有位置对象。

几秒钟后(有时是 2,有时是 20...),事件启动,并尝试结果。

有没有办法强制此事件,并在用户单击“确定”按钮后立即获取当前位置?

【问题讨论】:

  • 如果我认为获取位置可能需要一段时间,我对吗?也许这就是你的问题......顺便说一句,我没有看到任何吐司,它到底在哪里?
  • 我不能直接调用函数 onLocationChanged... 你也不应该。这不是它的工作原理。 有没有办法强制此事件,并在用户单击“确定”按钮后立即获取当前位置? 不,它不是这样工作的。你需要等待。你可以阅读更多hereofficial documentation..

标签: android google-api gps location locationmanager


【解决方案1】:

有没有办法强制此事件,并在用户单击“确定”按钮后立即获取当前位置?

没有。 GPS 本身需要一些时间来获得初始位置修复。这与 Android 无关,与 GPS 无关。

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多