如何显示使像谷歌地图的位置对话框? [英] How to show enable location dialog like Google maps?

查看:564
本文介绍了如何显示使像谷歌地图的位置对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌最新版本的播放服务(7.0),随后在他们的手册中的步骤,我启用像下面的位置对话框,其中有一个从不按钮。我的应用程序强制需要的位置,所以我不希望永远展现给用户,因为一旦用户点击从来没有,我无法获得位置或要求的位置再次可言。

I use the latest version of Google Play Services (7.0) and followed the steps given in their guide and I enabled the location dialog like below, which has a "never" button. My app mandatorily needs location so I don't want to show never to user, because once the user clicks "never", I'm unable to get location or request for location again at all.

如果谷歌地图只有yes和no按钮没有永远按钮,任何想法如何实现相同的?

Where as Google Maps has only yes and no button without never button, any idea how to achieve the same?

我的应用程序的形象

My app's image

谷歌地图的形象

Google Map's image

推荐答案

<一个href="https://developer.android.com/reference/com/google/android/gms/location/LocationSettingsRequest.Builder.html">LocationSettingsRequest.Builder有一个方法 setAlwaysShow(布尔显示)尽管文件表明它目前正在做什么(更新2015年7月5日:更新谷歌文档已经取消了这一措辞),设置 builder.setAlwaysShow(真); 将使谷歌地图的行为:

LocationSettingsRequest.Builder has a method setAlwaysShow(boolean show). While the document indicates it's currently doing nothing(updated 2015-07-05: updated Google documentation has removed this wording), setting builder.setAlwaysShow(true); will enable Google Maps behavior:

这里的code,它得到了它的工作:

Here's the code that got it working:

    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;
                    }
                }
            });             }

这篇关于如何显示使像谷歌地图的位置对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆