Windows 8 App XAML/C#:通过一种方法将多个图钉设置为Bing映射 [英] Windows 8 App XAML/C#: Set multiple pushpins to Bing map in one method

查看:98
本文介绍了Windows 8 App XAML/C#:通过一种方法将多个图钉设置为Bing映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力添加纬度&在Windows 8应用程序中使用XAML& C#.

I am struggling to add a collection of latitude & longitude as Pushpins on Bing Map, in a Windows 8 app using XAML & C#.

使用事件处理程序(例如在地图上右击)一一添加图钉,效果很好.

Adding Pushpins one by one using an event handler such as a right tap on the map works fine.

这是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);
        }
    }

以上所有作品.如果我点击地图,则会添加一个新的图钉.

All of the above works. If I tap the map, a new Pushpin is added.

现在,当我尝试通过迭代列表来初始化页面时尝试添加图钉时,地图上仅显示列表的最后一个图钉,好像每个新图钉都将覆盖前一个图钉一样.这是我使用的代码:

Now, when I try to add pushpin when initializing the page by iterating on a list, only the last Pushpin of the list is displayed on the map, as if each new Pushpin was overwriting the previous one. Here is the code I use:

 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循环将在每个位置完全执行.

As you can see, I use the same code, but at the end of the LoadState method, the map only displays the last location. In case you are wondering, the foreach loop is fully executed for each location.

是否有任何方法可以使它起作用,甚至更好,可以将地图子级直接绑定到Pushpin对象的ObservableCollection?我觉得我已经很近了,但是我无法弄清我错过了什么.

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.

请帮助!

推荐答案

您应该将不同的位置保留在数组(或列表或更有效的LocationCollection)中,并仅在循环遍历所有元素之后才调用SetView方法.

You should keep the different locations in an array (or a list or more efficient LocationCollection) and call the SetView method only after you've looped throught your elements.

  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));

这篇关于Windows 8 App XAML/C#:通过一种方法将多个图钉设置为Bing映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆