簡體   English   中英

Windows 8 App XAML / C#:在一種方法中將多個圖釘設置為Bing地圖

[英]Windows 8 App XAML/C#: Set multiple pushpins to Bing map in one method

在使用XAML&C#的Windows 8應用程序中,我正努力在Bing Map上添加緯度和經度集合作為Pushpins。

使用事件處理程序逐個添加圖釘,例如在地圖上右擊可以正常工作。

這是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? 我覺得我如此接近,但我無法弄清楚我錯過了什么。

請幫忙 !

您應該將不同的位置保留在數組(或列表或更有效的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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM