要求用户打开位置信息 [英] Ask user to turn on Location

查看:103
本文介绍了要求用户打开位置信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何提示用户打开位置?

该应用程序应该使用用户的当前位置过滤位置列表.如果用户关闭了 Location 服务,则应用程序应提示用户要求打开 Location .

The app is supposed to filter a list of locations with the current location of the user. If the user has the Location service turned off, the app should prompt the user asking for the Location to be turned on.

例如,旅行顾问应用程序执行此操作:

For instance Trip Advisor app does this:

(不知道我是否可以在此处发布其他应用程序的屏幕截图,但是如果我不应该这样做,请这样说.对大尺寸的图片表示歉意,尝试将其缩小,但这样没有做到. t喜欢它... )

在第一张图片中,您可以看到我已关闭位置服务.在打开 Trip Advisor 应用程序并点击立即靠近选项后,系统会提示我第二张图像,要求我在其中显示 Turn在位置服务上.点击按钮后,会出现一个对话框,我可以允许或禁止打开位置服务.如果我点击确定,则位置服务会在设备上打开,并且应用会使用它.

In the first image, you can see I have the Location service turned off. The, after opening the Trip Advisor app, and tapping the Near me now option, I'm prompted with the second image, where I'm asked to Turn on Location services. After I tap the button, a dialog shows up so I can allow, or disallow, the Location service to be turned on. If I tap OK, the Location service is turned on on the device, and the app consumes it.

我该如何实现?

推荐答案

找到了我要的解决方案.

Found the solution I was asking for.

要求

Nuget Xamarin.GooglePlayServices.Location

代码

Int64
    interval = 1000 * 60 * 1,
    fastestInterval = 1000 * 50;

try {
    GoogleApiClient
        googleApiClient = new GoogleApiClient.Builder( this )
            .AddApi( LocationServices.API )
            .Build();

    googleApiClient.Connect();

    LocationRequest
        locationRequest = LocationRequest.Create()
            .SetPriority( LocationRequest.PriorityBalancedPowerAccuracy )
            .SetInterval( interval )
            .SetFastestInterval( fastestInterval );

    LocationSettingsRequest.Builder
        locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
            .AddLocationRequest( locationRequest );

    locationSettingsRequestBuilder.SetAlwaysShow( false );

    LocationSettingsResult
        locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(
            googleApiClient, locationSettingsRequestBuilder.Build() );

    if( locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired ) {
        locationSettingsResult.Status.StartResolutionForResult( this, 0 );
    }
} catch( Exception exception ) {
    // Log exception
}

使用此代码,如果locationSettingsResult.Status.StatusCodeLocationSettingsStatusCodes.ResolutionRequired(6),则可能意味着 Location 已关闭,尽管我发现了一种情况当设备的选项关闭时,它没有返回值.打开和关闭后,它可以正常工作,可能是设备上的错误,还是没有.

With this code, if the locationSettingsResult.Status.StatusCode is LocationSettingsStatusCodes.ResolutionRequired ( 6 ) it means -- probably -- that the Location is turned off, although I've found one situation that it didn't return the value when the device had the option turned off. After turning on and off, it worked, might be a bug on the device, or not.

这篇关于要求用户打开位置信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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