2015-11-20 14 views
0

我嘗試添加多個圖釘到我的地圖,但他們不出現在地圖上! 正如我找出當我刪除DataTemplate標籤時,我看到它沒有任何Pin!添加圖釘從ObservableCollection地圖

我的XAML代碼:

<Window x:Class="Maps_test.AddPushpinToMap" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Maps_test" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
    Width="1024" Height="768"> 


<Grid Name="Main"> 
    <m:Map x:Name="myMap" 
      CredentialsProvider=" -" 
      Mode="AerialWithLabels" 
      Center="54.13,12.1004289" 
      ZoomLevel="12" 
      MouseDoubleClick="MapWithPushpins_MouseDoubleClick"> 

     <m:MapItemsControl x:Name="PushpinCollection" ItemsSource="{Binding PushpinList}"> 
      <m:MapItemsControl.ItemTemplate> 
       <DataTemplate> 
        <m:Pushpin Background="#FFB6DE2E" Tag="{Binding Tag}" Visibility="{Binding Show}"> 
         <m:Location Latitude="{Binding Latitude}" Longitude="{Binding Longitude}" /> 
        </m:Pushpin> 
       </DataTemplate> 
      </m:MapItemsControl.ItemTemplate> 
     </m:MapItemsControl> 

     <Button x:Name="button" Click="addImageToMap" Opacity="0.8" Grid.Row="1" HorizontalAlignment="Left" Height="24" Margin="10,52,0,0" VerticalAlignment="Top" Width="78"> 
      <TextBlock><Run Text="Show Image"/></TextBlock> 
     </Button> 
    </m:Map> 
    <Grid x:Name="menugrid" HorizontalAlignment="Left" Height="45" Width="{Binding ElementName=Main,Path=ActualWidth}" Margin="0,-1,0,0" VerticalAlignment="Top" > 
     <Menu x:Name="menu" Margin="0,2,-4,0"> 
      <Menu.Background> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="Black" Offset="0"/> 
        <GradientStop Color="#FF0C0909" Offset="1"/> 
       </LinearGradientBrush> 
      </Menu.Background> 
      <Label x:Name="label" Content="Rostocker Stadteil : " Height="23" Width="118" Foreground="#FFF9F4F4" FontFamily="Arial" Margin="0,10,0,0" /> 
      <ComboBox x:Name="comboBox" SelectionChanged="ChangeMapView_SelectionChanged" Height="23" Width="160" Margin="0,8,0,0"> 
       <ComboBoxItem Content="Seebad Warnemünde" Tag="54.1725,12.0828,0 15"/> 
       <ComboBoxItem Content="Diedrichshagen" Tag="54.171836,12.033028,0 15"/> 
       <ComboBoxItem Content="Markgrafenheide" Tag="54.190279,12.157802,0 15"/> 
       <ComboBoxItem Content="Hohe Düne" Tag="54.177790,12.116995,0 15"/> 
       <ComboBoxItem Content="Hinrichshagen" Tag="54.207616,12.196298,0 15"/> 
       <ComboBoxItem Content="Wiethagen" Tag="54.202428,12.252413,0 15"/> 
       <ComboBoxItem Content="Torfbrügge" Tag="54.233147,12.224683,0 15"/> 
       <ComboBoxItem Content="Lichtenhagen" Tag="54.155347,12.047310,0 15"/> 
       <ComboBoxItem Content="Groß Klein" Tag="54.149608,12.080472,0 15"/> 
       <ComboBoxItem Content="Lütten Klein" Tag="54.138211,12.052280,0 15"/> 
       <ComboBoxItem Content="Evershagen" Tag="54.118766,12.053579,0 15"/> 
       <ComboBoxItem Content="Schmarl" Tag="54.127447,12.080520,0 15"/> 
       <ComboBoxItem Content="Reutershagen" Tag="54.098111,12.072783,0 15"/> 
       <ComboBoxItem Content="Hansavirtel" Tag="54.084673,12.097592,0 15"/> 
       <ComboBoxItem Content="Gartenstadt/Stadtweide" Tag="54.076974,12.078598,0 15"/> 
       <ComboBoxItem Content="Kröpeliner-Tor-Vorstadt" Tag="54.095194,12.106574,0 15"/> 
       <ComboBoxItem Content="Südstadt" Tag="54.070748,12.121374,0 15"/> 
       <ComboBoxItem Content="Biestow" Tag="54.060217,12.098262,0 15"/> 
       <ComboBoxItem Content="Stadtmitte" Tag="54.085435,12.141921,0 15"/> 
       <ComboBoxItem Content="Brinkmansdorf" Tag="54.087047,12.175463,0 15"/> 
       <ComboBoxItem Content="Dierkow" Tag="54.100835,12.162007,0 15"/> 
       <ComboBoxItem Content="Toitenwinkel" Tag="54.116951,12.141470,0 15"/> 
       <ComboBoxItem Content="Gehldorf" Tag="54.104504,12.120084,0 15"/> 
       <ComboBoxItem Content="Hinrichsdorf" Tag="54.132611,12.158463,0 15"/> 
       <ComboBoxItem Content="Krummendorf" Tag="54.128727,12.121321,0 15"/> 
       <ComboBoxItem Content="Nienhagen" Tag="54.147796,12.173734,0 15"/> 
       <ComboBoxItem Content="Peez" Tag="54.148566,12.125750,0 15"/> 
       <ComboBoxItem Content="Stuthof" Tag="54.166652,12.173001,0 15"/> 
       <ComboBoxItem Content="Jürgeshof" Tag="54.177121,12.189099,0 15"/> 
      </ComboBox> 
      <Label x:Name="label1" Content="Umkreis :" Height="23" Width="64" Foreground="#FFF9F4F4" Margin="0,10,0,0"/> 
      <Slider x:Name="Distance" Width="100" BorderThickness="0,10,0,0" Value="1"/> 
      <Canvas Height="33" Width="265" Margin="0,5,0,0"> 
       <ComboBox x:Name="EntityType" SelectionChanged="ShowPins_SelectionChanged" Height="23" Canvas.Left="98" Canvas.Top="10" Width="129"> 
        <ComboBoxItem Tag="7011" IsSelected="True" >Hotels</ComboBoxItem> 
        <ComboBoxItem Tag="5800" >Tourist Attractions</ComboBoxItem> 
        <ComboBoxItem Tag="7999" >Restaurants</ComboBoxItem> 
       </ComboBox> 
       <Label x:Name="label2" Content="POIS:" Height="23" Canvas.Left="11" Canvas.Top="10" Width="71" Foreground="Gray"/> 
      </Canvas> 

     </Menu> 
    </Grid> 
</Grid> 

PushpinViewModel代碼

 public sealed partial class MainPage : Window 
    { 
     public MainPage() 
     { 
      PushpinViewModel pvm = new PushpinViewModel(); 
      pvm.PushpinList = new ObservableCollection<PushpinClass>(); 

      pvm.PushpinList.Add(new PushpinClass() { Latitude = 54.090604, Longitude = 12.147961, Tag = "5800", Show = Visibility.Visible }); 
      pvm.PushpinList.Add(new PushpinClass() { Latitude = 54.1581419, Longitude = 12.0763132, Tag = "7999", Show = Visibility.Visible }); 
      pvm.PushpinList.Add(new PushpinClass() { Latitude = 14f, Longitude = 12.0763132, Tag = "7011", Show = Visibility.Visible }); 
      DataContext = pvm; 
     } 
    } 

    class PushpinViewModel 
    { 
     public ObservableCollection<PushpinClass> PushpinList { get; set; } 
    } 

    class PushpinClass 
    { 
     public double Longitude { get; set; } 
     public double Latitude { get; set; } 
     public string Tag { get; set; } 
     public Visibility Show { get; set; } 
     public ToolTip Tip { get; set; } 

    } 

我MAINWINDOW.XAML.CS CODE

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media.Imaging; 
using Microsoft.Maps.MapControl.WPF; 
using Microsoft.Maps.MapControl.WPF.Design; 
using System.Globalization; 
using System.Xml; 
using System.Net; 

namespace Maps_test 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
     public partial class MainWindow : Window 
     { 
      public MainWindow() 
      { 

      } 
     } 

    public enum TagInfo { Hotels = 7011, Restaurants = 7999, TouristAttractions = 5800 } 
    public partial class AddPushpinToMap : Window 
    { 
     LocationConverter locConverter = new LocationConverter(); 
     string BingMapsKey = "_"; 

     public AddPushpinToMap() 
     { 
      InitializeComponent(); 

      //Set focus on mapl 
      myMap.Focus(); 
     } 



     private void MapWithPushpins_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      // Disables the default mouse double-click action. 
      e.Handled = true; 

      // Determin the location to place the pushpin at on the map. 

      //Get the mouse click coordinates 
      Point mousePosition = e.GetPosition(this); 
      //Convert the mouse coordinates to a locatoin on the map 
      Location pinLocation = myMap.ViewportPointToLocation(mousePosition); 

      // The pushpin to add to the map. 
      Pushpin pin = new Pushpin(); 
      pin.Location = pinLocation; 

      // Adds the pushpin to the map. 
      myMap.Children.Add(pin); 
     } 

     private void addImageToMap(object sender, RoutedEventArgs e) 
     { 
      MapLayer imageLayer = new MapLayer(); 


      Image image = new Image(); 
      image.Height = 150; 
      //Define the URI location of the image 
      BitmapImage myBitmapImage = new BitmapImage(); 
      myBitmapImage.BeginInit(); 
      myBitmapImage.UriSource = new Uri("https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/Dortmund_Petrikirche.jpg/220px-Dortmund_Petrikirche.jpg"); 
      // To save significant application memory, set the DecodePixelWidth or 
      // DecodePixelHeight of the BitmapImage value of the image source to the desired 
      // height or width of the rendered image. If you don't do this, the application will 
      // cache the image as though it were rendered as its normal size rather then just 
      // the size that is displayed. 
      // Note: In order to preserve aspect ratio, set DecodePixelWidth 
      // or DecodePixelHeight but not both. 
      //Define the image display properties 
      myBitmapImage.DecodePixelHeight = 150; 
      myBitmapImage.EndInit(); 
      image.Source = myBitmapImage; 
      image.Opacity = 0.6; 
      image.Stretch = System.Windows.Media.Stretch.None; 

      //The map location to place the image at 
      Location location = new Location() { Latitude = 54.090604, Longitude = 12.147961 }; 
      //Center the image around the location specified 
      PositionOrigin position = PositionOrigin.Center; 

      //Add the image to the defined map layer 
      imageLayer.AddChild(image, location, position); 
      //Add the image layer to the map 
      myMap.Children.Add(imageLayer); 
     } 

     private void viewMap_ViewChangeOnFrame(object sender, MapEventArgs e) 
     { 
      // Gets the map object that raised this event. 
      Map map = sender as Map; 
      // Determine if we have a valid map object. 
      if (map != null) 
      { 
       // Gets the center of the current map view for this particular frame. 
       Location mapCenter = map.Center; 

      /* // Updates the latitude and longitude values, in real time, 
       // as the map animates to the new location. 
       txtLatitude.Text = string.Format(CultureInfo.InvariantCulture, 
        "{0:F5}", mapCenter.Latitude); 
       txtLongitude.Text = string.Format(CultureInfo.InvariantCulture, 
        "{0:F5}", mapCenter.Longitude);*/ 
      } 
     } 


     private void ChangeMapView_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      ComboBoxItem cbi = (ComboBoxItem)(((ComboBox)sender).SelectedItem); 

      // Parse the information of the Combobox Tag property 
      string[] tagInfo = cbi.Tag.ToString().Split(' '); 
      Location center = (Location)locConverter.ConvertFrom(tagInfo[0]); 
      double zoom = System.Convert.ToDouble(tagInfo[1]); 

      // Set the map view 
      myMap.SetView(center, zoom); 
     } 

     // Geocode an address and return a latitude and longitude 
     public XmlDocument Geocode(string addressQuery) 
     { 
      //Create REST Services geocode request using Locations API 
      string geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + addressQuery + "?o=xml&key=" + BingMapsKey; 

      //Make the request and get the response 
      XmlDocument geocodeResponse = GetXmlResponse(geocodeRequest); 

      return (geocodeResponse); 
     } 


     // Submit a REST Services or Spatial Data Services request and return the response 
     private XmlDocument GetXmlResponse(string requestUrl) 
     { 
      System.Diagnostics.Trace.WriteLine("Request URL (XML): " + requestUrl); 
      HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; 
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
      { 
       if (response.StatusCode != HttpStatusCode.OK) 
        throw new Exception(String.Format("Server error (HTTP {0}: {1}).", 
        response.StatusCode, 
        response.StatusDescription)); 
       XmlDocument xmlDoc = new XmlDocument(); 
       xmlDoc.Load(response.GetResponseStream()); 
       return xmlDoc; 
      } 
     } 

     public void ShowPins_SelectionChanged(object sender, SelectionChangedEventArgs e) 


// Is part is not ready 
     { 
      ComboBoxItem pin = (ComboBoxItem)(((ComboBox)sender).SelectedItem); 

      string tagInfo = pin.Tag.ToString(); 

      switch(tagInfo) 
      { 
       case "7011": 



        break; 
      } 
     } 

    } 

我已經找了解決方案,但所有的i找到d oesnt工作。 PLZ幫助!

回答

0

MapItemsControl自ItemsControl類繼承,所以你必須綁定ItemsSource屬性:

<m:MapItemsControl ItemsSource="{Binding PushpinList}"/> 

編輯: 除了上面你有你的類公開:

public class PushpinViewModel{...} 
public class PushpinClass{...} 
+0

謝謝!忘記在上面的代碼中插入行沒有工作。 – Roland

+0

嘿@羅蘭!類PushpinViewModel和類PushpinClass應該是公共的。 Remenber默認在c#中所有的類都是私人的,除非你指定了另一個作用域(public,protected) –

+0

哦還沒有看到它!謝謝! ( – Roland