本文介绍了如何使用Xamarin表单提示用户进行地理位置定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xamarin表单应用程序中的一个应用程序,该应用程序需要征求地理位置许可,如果被授予,则需要从设备获取地理位置数据,然后将地理位置坐标放入Forecast.io URL中,我正在使用Geolocator当我打开雷达页面时,James Montemagno的插件以及James Montemagno的PermissionsPlugin插件会显示白色,它永远不会征求我的同意,这是我的xamarin表单代码:

I am working on a app in xamarin forms app that needs ask for the geolocation permission and if granted it needs to get the geolocation data from the device and then put the geolocation coordinates into the forecast.io URL I am using the Geolocator plugin by James Montemagno as well as the PermissionsPlugin by James Montemagno when I open the radar page the screen just stays white it never ask's for my permission here's my xamarin forms code:

using AppName.Data;
using Xamarin.Forms;
using Plugin.Geolocator;
using System.Diagnostics;
using System.Threading.Tasks;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using System;

namespace AppName.Radar
{
    public partial class RadarHome : ContentPage
    {
        public RadarHome()
        {
            InitializeComponent();
        }

         async void locator()
        {
            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
                if (status != PermissionStatus.Granted)
                {
                    if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                    {
                        await DisplayAlert("Need location", "Gunna need that location", "OK");
                    }

                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
                    status = results[Permission.Location];
                }

                if (status == PermissionStatus.Granted)
                {
                    var browser = new WebView();
                    var results = await CrossGeolocator.Current.GetPositionAsync(10000);
                    browser.Source  = "https://forecast.io/?mobile=1#/f/" + "Lat: " + results.Latitude + " Long: " + results.Longitude;
                }
                else if (status != PermissionStatus.Unknown)
                {
                    await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
            }
        }
    }
}

这是我的Android MainActivity代码:

and here's my Android MainActivity code:

using Android.App;
using Android.Content.PM;
using Android.OS;
using Xamarin.Forms.Platform.Android;
using Android;
using Plugin.Permissions;

namespace AppName.Droid
{
    [Activity(MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/CustomTheme")]

    public class MainActivity : FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

我遗漏了一些东西吗?这两个插件都安装在android项目和iOS项目这两个表单项目中

Am I leaving something out? both plugins are installed in both the forms project the android project and the iOS project

推荐答案

您需要添加所需的权限.

You need to add the required permissions.

Android 中,要允许我们的应用访问位置服务,我们需要启用两个Android权限:ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION.

In Android, to allow our application to access location services, we need to enable two Android permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

iOS 中,根据您是始终使用地理位置(例如地图应用程序)还是仅在用户工作流程中的某些时间点,您需要添加键Info.plist中的>或NSLocationAlwaysUsageDescription,以及用于密钥的新字符串条目,该字符串准确描述了您将对用户的位置执行的操作.在运行时向用户提示权限时,将显示此处列出的说明.

In iOS, Depending on if you will be always using geolocation (such as a maps app), or just at certain points in a user’s workflow, you will either need to add the key NSLocationWhenInUsageDescription or NSLocationAlwaysUsageDescription in your Info.plist, along with a new string entry for the key that describes exactly what you’ll be doing with the user’s location. When the permission is prompted to the user at runtime, the description listed here will display.

在Windows中,必须启用ID_CAP_LOCATION权限.

In Windows, the ID_CAP_LOCATION permission must be enabled.

在此处阅读有关此主题的完整博客-地理位置适用于iOS,Android和Windows的

Read the full blog about it here - Geolocation for iOS, Android, and Windows Made Easy

这篇关于如何使用Xamarin表单提示用户进行地理位置定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:07