【发布时间】:2013-02-18 12:27:17
【问题描述】:
我正在努力在使用 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 循环会针对每个位置完全执行。
有没有办法让它工作,或者甚至更好地直接将地图子项绑定到 Pushpin 对象的 ObservableCollection ?我觉得我很接近,但我无法弄清楚我错过了什么。
请帮忙!
【问题讨论】:
标签: c# windows-8 winrt-xaml bing-maps pushpin