[英]Xamarin Forms Geolocator IOS
我正在研究的項目遇到問題。 我最近開始學習C#,並已成功地在Xamarin Forms中構建了一個基本程序。
我正在潛入一個相當大的項目,並繼續學習。
我當前遇到的問題是生成一個以設備當前位置為中心的地圖頁面。 我將問題范圍縮小到了我認為的位置,即一旦獲得該位置,就不會將其保存到我聲明的變量中,以便可以用另一種方法訪問它。
public partial class MapPage : ContentPage
{
public static double latitude;
public static double longitude;
public MapPage()
{
InitializeComponent();
GetCurrentLocation();
var map = new Map(MapSpan.FromCenterAndRadius(
new Position(latitude, longitude), Distance.FromMiles(.3)))
{
MapType = MapType.Street,
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand,
HasScrollEnabled = true,
HasZoomEnabled = true
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(map);
Content = stack;
}
private static async void GetCurrentLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
latitude = position.Latitude;
longitude = position.Longitude;
}
}
使用上面的代碼顯示地圖時,它將收到經度(0,0)。 我已經使用斷點和調試進行了驗證,正在檢索緯度和經度並將其存儲在“ position”變量中,但是我不認為它會存儲在我的緯度和經度變量中。
對我在這里想念的東西有任何想法嗎?
public async override void OnAppearing()
{
var pos = await GetCurrentLocation();
var map = new Map(MapSpan.FromCenterAndRadius(
pos, Distance.FromMiles(.3)))
{
MapType = MapType.Street,
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand,
HasScrollEnabled = true,
HasZoomEnabled = true
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(map);
Content = stack;
}
private async Position GetCurrentLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
return position;
}
在代碼中應注意以下幾點 :
檢索位置的方法“ GetCurrentLocation()”是異步的。 如果失敗,則您的應用程序失敗。 您應該在此方法內少使用try {} catch {}。 就個人而言,出於可讀性考慮,我還將“ Async”后綴添加到異步方法中(即:GetCurrentLoactionAsync())。
接下來,從構造函數中調用“異步”方法。 但是在構造函數中,您無法等待異步方法(等待結果)。 這就是為什么您總是空頭的原因。 您不應該從這里調用此方法。
如@Jason所述,您可以重寫'OnAppearing()'方法來加載數據。 這是一個更好的地方。 但是請小心使用“ await”關鍵字,並在try / catch塊中調用該方法,因為“ OnAppearing()”不會返回Task。
最后,我建議您在XAML代碼部分中創建地圖(因此,地圖已經存在,並且在加載位置之前不會有空白頁)。 然后,在OnAppearing()中使用posistion初始化地圖(它可能會通過重新居中在地圖上生成動畫。我認為它更令人愉快;)。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MediaSample.MapPage">
<ContentPage.Content>
<Grid>
<!-- your map object -->
<Map x:Name="uiMap" />
<!-- whatever you want -->
</Grid>
</ContentPage.Content>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPage : ContentPage
{
public MapPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
try
{
// get your position
var centerPos = await GetCurrentLocation();
// init map => center on the position
uiMap.MoveToRegion(MapSpan.FromCenterAndRadius(centerPos, Distance.FromMiles(.3)).WithZoom(20));
}
catch(Exception e)
{
// notify user here
// log exception
}
}
private async Task<Position> GetCurrentLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
return position;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.