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

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

问题描述

如何提示用户开启位置?

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

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.

例如,Trip Advisor 应用就是这样做的:

For instance Trip Advisor app does this:

( 不确定我是否可以在此处发布其他应用程序的屏幕截图,但如果我不应该这样做,请说出来.并对全尺寸图像表示歉意,试图将它们缩小,但没有这样做'不喜欢... )

在第一张图片中,您可以看到我关闭了位置服务.在打开 Trip Advisor 应用程序并点击 Near me now 选项后,系统会提示我第二张图片,要求我转向定位服务.点击按钮后,会出现一个对话框,以便我可以允许或禁止打开位置服务.如果我点按确定位置服务将在设备上打开,并且应用会使用它.

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.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

找到了我想要的解决方案.

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 ) 这意味着 - 可能 -位置 已关闭,尽管我发现了一种情况,即当设备关闭该选项时它没有返回值.打开和关闭后,它起作用了,可能是设备上的错误.

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天全站免登陆