使用Xamarin返回0获取当前gps位置

问题描述 投票:0回答:1

我尝试使用Xamarin在Android上获取当前位置。我使用谷歌地图和以下代码:

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace WorldHerp
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        private double _latitude = 0.0;
        private double _longitude = 0.0;

        public MainPage()
        {
            InitializeComponent();
            Task.Run(async () => { await GetCurrentLocationAsync(); });
            MapView.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(_latitude, _longitude), Distance.FromKilometers(1)).WithZoom(20));
        }

        private async Task GetCurrentLocationAsync()
        {
            try
            {
                var location = await Geolocation.GetLocationAsync();

                if (location != null)
                {
                    _latitude = location.Latitude;
                    _longitude = location.Longitude;
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                await DisplayAlert("Faild", fnsEx.Message, "OK");
            }
            catch (PermissionException pEx)
            {
                await DisplayAlert("Faild", pEx.Message, "OK");
            }
            catch (Exception ex)
            {
                await DisplayAlert("Faild", ex.Message, "OK");
            }
        }
    }
}

他们只为我的_latitude / _longitude变量返回0。在尝试{}的时候进行了调试,就好像他从不进屋,但没有显示任何错误。

你有什么想法吗?

感谢每前进一步

c# android xamarin geolocation gps
1个回答
0
投票

您需要有一个异步非空方法来等待结果。

>

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace WorldHerp
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();            
        }

        protected async override void OnAppearing()
        {
            var position = await GetCurrentLocationAsync();
            MapView.MoveToRegion(MapSpan.FromCenterAndRadius((position != null ? 
                                                             position : 
                                                             new Position(0.0, 0.0)), 
                                                             Distance.FromKilometers(1)).WithZoom(20));
            base.OnAppearing();
        }

        private async Task<Xamarin.Essentials.Location> GetCurrentLocationAsync()
        {
            try
            {
                return await Geolocation.GetLocationAsync();
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                await DisplayAlert("Faild", fnsEx.Message, "OK");
            }
            catch (PermissionException pEx)
            {
                await DisplayAlert("Faild", pEx.Message, "OK");
            }
            catch (Exception ex)
            {
                await DisplayAlert("Faild", ex.Message, "OK");
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.