我希望Windows Phone上的地图控件逐个在坐标之间移动。我似乎无法让地图控件等到动画完成到达一个位置,然后再尝试移动到下一个位置。我已经尝试了几种方法让它在两个动作之间等待无济于事。这是我到目前为止的示例应用程序的代码。
public MainPage()
{
InitializeComponent();
map.Center = new GeoCoordinate(54.958879, -7.733027);
map.ZoomLevel = 13;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
List<Location> locations = new List<Location>();
for (double x = 0, y = 0; y < 10; x+=0.005, y++)
{
locations.Add(new Location(54.958879 + x, -7.733027 + x));
locations.Add(new Location(54.958879 - x, -7.733027 - x));
}
foreach (Location location in locations)
{
map.SetView(new GeoCoordinate(location.Latitude, location.Longitude), 13, MapAnimationKind.Linear);
//I want the app to wait until this view has finished moving before moving again
}
}
class Location
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public Location(double lat, double lon)
{
Latitude = lat;
Longitude = lon;
}
}
我确信我错过了一些简单的事情。有人可以帮助解决这个问题吗?
答案 0 :(得分:1)
我会使用DispatcherTimer
并在Tick
事件中,迭代到下一个坐标,然后拨打SetView
。
例如:
private DispatcherTimer timer;
private int index = 0;
List<Location> locations = new List<Location>();
public MainPage()
{
InitializeComponent();
timer = new System.Windows.Threading.DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1) // TODO: your interval
};
timer.Tick += timer_Tick;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
locations.Clear();
for (double x = 0, y = 0; y < 10; x+=0.005, y++)
{
locations.Add(new Location(54.958879 + x, -7.733027 + x));
locations.Add(new Location(54.958879 - x, -7.733027 - x));
}
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
var item = locations[index];
map.SetView(new Geocoordinate(item.Latitude, item.Longitude), 13, MapAnimationKind.Linear);
if(index >= locations.Count)
timer.Stop();
else
index++;
}