2013-02-18 76 views
0

我很努力地添加经纬度集合&作为图钉上的图钉,在Windows 8应用程序中使用XAML & C#。Windows 8应用程序XAML/C#:设置多个图钉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循环。

有没有什么办法让这个工作,甚至更好,直接绑定地图的孩子ObservableCollection Pushpin对象?我感觉自己如此亲密,但我无法弄清楚我错过了什么。

请帮忙!

回答

2

您应该将数组中的不同位置(或列表或更有效的LocationCollection)保留下来,并且只有在遍历元素后才调用SetView方法。

LocationCollection locationCollection = new LocationCollection(); 
    // 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); 


     locationCollection.Append(location); 
    } 
    myMap.SetView(new LocationRect(locationCollection)); 
+0

感谢你回答 – 2013-02-18 22:37:06

+0

对不起,这一次我登录到能够做进一步的评论......我想你建议(感谢LocationCollection类,我不知道有这样的集合) 。但这并没有改变我的问题:locationCollection确实被正确填充(有49个位置),但地图上只显示一个单独的图钉。 – 2013-02-18 22:46:30

+0

您确定有不同的位置添加到集合中。您是否将SetView()调用移出循环? 验证有关坐标值的locationCollection的内容。 – 2013-02-19 00:34:13

相关问题