使用Xamarin表格进行简单的地图跟踪,地图一直闪烁:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
GetCurrentPosition();
}
public async void GetCurrentPosition()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
locator.PositionChanged += (s, e) =>
{
var map = new Map(
MapSpan.FromCenterAndRadius(
new Position(e.Position.Latitude, e.Position.Longitude), Distance.FromMiles(0.3)))
{
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(map);
Content = stack;
};
if (!locator.IsListening)
{
await locator.StartListeningAsync(1000, 0);
}
}
}
我该如何解决?
答案 0 :(得分:0)
这是正常的。这是因为每次更新位置时,您都会创建一个添加到页面的新地图(因此闪烁)。相反,您应该保留地图的相同实例并更新此地图的位置。
你可以试试这个:
public partial class MainPage : ContentPage
{
private Map _map;
public MainPage()
{
InitializeComponent();
_map = new Map
{
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(_map);
Content = stack;
GetCurrentPosition();
}
public async void GetCurrentPosition()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
locator.PositionChanged += (s, e) =>
{
_map.MoveToRegion(
MapSpan.FromCenterAndRadius(
new Position(e.Position.Latitude, e.Position.Longitude), Distance.FromMiles(0.3)));
};
if (!locator.IsListening)
{
await locator.StartListeningAsync(1000, 0);
}
}
}