我正在努力在使用 XAML 和 C# 的 Windows 8 应用程序中将纬度和经度集合添加为 Bing 地图上的图钉。
使用事件处理程序(例如在地图上右击)一一添加图钉可以正常工作。
这是 XAML:
<bm:Map x:Name="myMap" Grid.Row="0" MapType="Road" ZoomLevel="14" Credentials="{StaticResource BingMapAPIKey}" ShowTraffic="False" Tapped="map_Tapped" >
<bm:Map.Center>
<bm:Location Latitude="-37.812751" Longitude="144.968204" />
</bm:Map.Center>
</bm:Map>
这是处理程序:
private void map_Tapped(object sender, TappedRoutedEventArgs e)
{
// Retrieves the click location
var pos = e.GetPosition(myMap);
Bing.Maps.Location location;
if (myMap.TryPixelToLocation(pos, out location))
{
// Place Pushpin on the Map
Pushpin pushpin = new Pushpin();
pushpin.RightTapped += pushpin_RightTapped;
MapLayer.SetPosition(pushpin, location);
myMap.Children.Add(pushpin);
// Center the map on the clicked location
myMap.SetView(location);
}
}
以上所有工作。如果我点击地图,则会添加一个新的图钉。
现在,当我尝试通过迭代列表来初始化页面时添加图钉时,地图上只显示列表的最后一个图钉,就好像每个新图钉都覆盖了前一个图钉一样。这是我使用的代码:
protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
...
// The Venue class is a custom class, the Latitude & Logitude are of type Double
foreach (Venue venue _venues)
{
Bing.Maps.Location location = new Location(venue.Latitude, venue.Longitude);
// Place Pushpin on the Map
Pushpin pushpin = new Pushpin();
pushpin.RightTapped += pushpin_RightTapped;
MapLayer.SetPosition(pushpin, location);
myMap.Children.Add(pushpin);
// Center the map on the clicked location
myMap.SetView(location);
}
...
}
如您所见,我使用相同的代码,但在 LoadState 方法的末尾,地图只显示最后一个位置。如果您想知道,foreach 循环会针对每个位置完全执行。
Is there any way to get this to work, or even better, to bind directly the map children to a ObservableCollection of Pushpin objects ? I feel like I am so close but I can't figure out what I missed.
Please help !