Googleマップのような位置情報ダイアログを表示するにはどうすればいいですか? 質問する

Googleマップのような位置情報ダイアログを表示するにはどうすればいいですか? 質問する

私は最新バージョンの Google Play Services (7.0) を使用しており、ガイドに記載されている手順に従い、以下のように「なし」ボタンがある位置情報ダイアログを有効にしました。私のアプリは位置情報を必須に必要とするため、ユーザーに「なし」を表示したくありません。ユーザーが「なし」をクリックすると、位置情報を取得したり、位置情報を再度要求したりすることができなくなるためです。

Google マップには「はい」と「いいえ」のボタンしかなく、「しない」ボタンがないのですが、同じことを実現する方法をご存知ですか?

私のアプリの画像ここに画像の説明を入力してください

Googleマップの画像ここに画像の説明を入力してください

ベストアンサー1

ロケーション設定リクエスト.ビルダーにはメソッドがありますsetAlwaysShow(boolean show) ドキュメントでは、現在は何も行われていないと示されていますが (2015-07-05 更新: 更新された Google ドキュメントではこの文言が削除されました)、設定によりbuilder.setAlwaysShow(true);Google マップの動作が有効になります。ここに画像の説明を入力してください

これが動作するコードです:

if (googleApiClient == null) {
    googleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    googleApiClient.connect();

    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); //this is the key ingredient
    //**************************

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, 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.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                getActivity(), 1000);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}

Androidドキュメントより

Kotlin についてはこちらをご覧ください:https://stackoverflow.com/a/61868985/12478830

おすすめ記事