How to display/hide PushPins based on a database field?

Locked How to display/hide PushPins based on a database field?

  • Donnerstag, 26. Mai 2011 13:39
     
      Enthält Code
    Hi, I am working on a Bing Maps project and using multiple PushPins. How do I Hide/Display them based on the U.S. State they are in?
    My code is below;
    <pre lang="x-xml"><UserControl x:Class="Demo1.MainPage"
    
     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:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
    
     xmlns:t="clr-namespace:Demo1"
    
     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    
     <UserControl.Resources>
    
     <DataTemplate x:Key="LogoTemplate">
    
      <m:Pushpin m:MapLayer.Position="{Binding Location}" Background="Blue">
    
      <m:Pushpin.RenderTransform>
    
       <ScaleTransform ScaleX=".25" ScaleY=".25" CenterX="17" CenterY="35"></ScaleTransform>
    
      </m:Pushpin.RenderTransform>
    
      <m:Pushpin.Content>
    
       <TextBlock x:Name="txtCustId" FontSize="7" Text="{Binding CustomerId}" Margin="2,2,2,2"></TextBlock>
    
      </m:Pushpin.Content>
    
      <ToolTipService.ToolTip >
    
       <Border MinHeight="40" MaxHeight="250" Margin="-10,-5,-10,-5" MinWidth="150"
    
    					Background="WhiteSmoke"
    
    					Opacity="1" VerticalAlignment="Top"
    
    					BorderBrush="Black" 
    
    					BorderThickness="1"
    
    					CornerRadius="10">
    
       <StackPanel>
    
       <Grid >
    
       <Grid.RowDefinitions>
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
        <RowDefinition Height="*" />
    
       </Grid.RowDefinitions>
    
       <Grid.ColumnDefinitions>
    
        <ColumnDefinition Width="*" />
    
        <ColumnDefinition Width="*" />
    
       </Grid.ColumnDefinitions>
    
       <TextBlock Text="Location ID:" Grid.Column="0" Grid.Row="0" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLocationID" Grid.Column="1" Grid.Row="0" Text="{Binding LocationID}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Location Sub:" Grid.Column="0" Grid.Row="1" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLocationSub" Grid.Column="1" Grid.Row="1" Text="{Binding LocationSub}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Location Name:" Grid.Column="0" Grid.Row="2" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLocationName" Grid.Column="1" Grid.Row="2" Text="{Binding LocationName}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="State:" Grid.Column="0" Grid.Row="3" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLocationState" Grid.Column="1" Grid.Row="3" Text="{Binding LocationState}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Status:" Grid.Column="0" Grid.Row="4" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLocationStatus" Grid.Column="1" Grid.Row="4" Text="{Binding LocationStatus}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Latitude:" Grid.Column="0" Grid.Row="5" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLatLocation" Grid.Column="1" Grid.Row="5" Text="{Binding Location.Latitude}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Longitude:" Grid.Column="0" Grid.Row="6" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtLongLocation" Grid.Column="1" Grid.Row="6" Text="{Binding Location.Longitude}" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock Text="Notes:" Grid.Column="0" Grid.Row="7" Margin="2,2,2,2"></TextBlock>
    
       <TextBlock x:Name="txtNotes" Grid.Column="0" Grid.Row="7" Text="{Binding Notes}" Margin="2,2,2,2"></TextBlock>
    
       </Grid>
    
      </StackPanel>
    
      </Border>
    
      </ToolTipService.ToolTip>
    
      </m:Pushpin>
    
     </DataTemplate>
    
     <t:LocationDataCollection x:Key="LocationList" IsDataSource="True"/>
    
    
    
     </UserControl.Resources>
    
     <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    
     <Grid Grid.Row="1">
    
      
    
     </Grid>
    
      <m:Map CredentialsProvider="Bing Maps Key" HorizontalAlignment="Stretch" LogoVisibility="Collapsed" VerticalAlignment="Stretch" x:Name="MyMap" Grid.Column="1" Grid.Row="0" >
    
      <m:MapItemsControl x:Name="ListOfItems"
    
       ItemTemplate="{StaticResource LogoTemplate}"
    
       ItemsSource="{StaticResource LocationList}">
    
      <!--ItemsSource="{StaticResource LocationList}">-->
    
      </m:MapItemsControl>
    
      <m:MapLayer x:Name="PushPinLayer"/>
    
     </m:Map>
    
     </Grid>
    
    </UserControl>
    
    
    
    
    <pre lang="x-c#">using System.Windows.Controls;
    
    using Microsoft.Maps.MapControl;
    
    
    
    namespace Demo1
    
    {
    
     public partial class MainPage : UserControl
    
     {
    
     public MainPage()
    
     {
    
      InitializeComponent();
    
    
    
      MyMap.Center = new Location(38.4793, -98.4814);
    
      MyMap.ZoomLevel = 5; 
    
     }
    
     }
    
    }
    
    
    
    
     
    <pre lang="x-c#">using System;
    
    using System.Collections.Generic;
    
    using System.Configuration;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Linq;
    
    using System.Runtime.Serialization;
    
    using System.ServiceModel;
    
    using System.ServiceModel.Activation;
    
    using System.Text;
    
    using Demo1.Web;
    
    
    
    namespace Demo1.Web
    
    {
    
     [ServiceContract(Namespace = "")]
    
     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    
    
    
     public class DataService 
    
     {
    
     static string connectionString = ConfigurationManager.ConnectionStrings["PTC"].ConnectionString;
    
    
    
     [OperationContract]
    
     public List<Locations> GetLocations()
    
     {
    
      SqlConnection sqlConnection = new SqlConnection(connectionString);
    
      DataSet objSet = new DataSet();
    
    
    
      SqlCommand sqlCommand = new SqlCommand();
    
      sqlCommand.Connection = sqlConnection;
    
      sqlCommand.CommandText = "GetMapLocations";
    
      sqlCommand.CommandType = CommandType.StoredProcedure;
    
    
    
      SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
    
      sqlDataAdapter.SelectCommand = sqlCommand;
    
      sqlDataAdapter.Fill(objSet);
    
    
    
      List<Locations> lstResult = new List<Locations>();
    
      Locations objLocations;
    
    
    
      if (objSet.Tables.Count > 0)
    
      {
    
      foreach (DataRow dr in objSet.Tables[0].Rows)
    
      {
    
       objLocations = new Locations();
    
       objLocations.LocationID = Convert.ToInt32(dr["LocationID"]);
    
       objLocations.LocationSub = dr["LocationSub"].ToString();
    
       objLocations.LocationName = dr["LocationName"].ToString();
    
       objLocations.LocationState = dr["LocationState"].ToString();
    
       objLocations.LocationStatus = dr["LocationStatus"].ToString();
    
       objLocations.Latitude = Convert.ToDouble(dr["Latitude"]);
    
       objLocations.Longitude = Convert.ToDouble(dr["Longitude"]);
    
       objLocations.Notes = dr["Notes"].ToString();
    
       lstResult.Add(objLocations);
    
      }
    
      }
    
      return lstResult;
    
     }
    
     }
    
    }
    
    
    
    
    using System;
    
    using System.Collections;
    
    using System.Collections.Generic;
    
    using System.Collections.ObjectModel;
    
    using System.IO;
    
    using System.Net;
    
    using System.Reflection;
    
    using System.Xml;
    
    using System.Xml.Serialization;
    
    using Microsoft.Maps.MapControl;
    
    using System.Windows.Media;
    
    namespace Demo1
    
    {
    
     /// <summary>
    
     /// This class contains an embedded Location object. It acts as DataContext
    
     /// for the individual items within MapItemsControl. Thus {Binding Location} translates to LocationData.Location
    
     /// </summary>
    
    
    
     public class LocationData
    
     {
    
     public Location Location
    
     { get; set; }
    
     
    
     public String LocationSub
    
     { get; set; }
    
     
    
     public String LocationName
    
     { get; set; }
    
     
    
     public String LocationState
    
     { get; set; }
    
     
    
     public String LocationStatus
    
     { get; set; }
    
     
    
     public Int32 LocationID
    
     { get; set; }
    
    
    
     public ObservableCollection<LocationData> Locations
    
     { get; set; }
    
    
    
     public LocationData()
    
     {
    
      this.Location = new Location();
    
     }
    
     public String Notes
    
     { get; set; }
    
     }
    
     /// <summary>
    
     /// This class exposes IEnumerable, and acts as ItemsSource for 
    
     /// MapItemsControl
    
     /// </summary>
    
     public class LocationDataCollection : ObservableCollection<LocationData>
    
     {
    
     public bool IsDataSource
    
     {
    
      get
    
      {
    
      return true;
    
      }
    
      set
    
      {
    
      this.Load();
    
      }
    
     }
    
    
    
     public LocationDataCollection()
    
     {
    
    
    
     }
    
    
    
     public void Load()
    
     {
    
      Demo1.DataService.DataServiceClient objCust = new Demo1.DataService.DataServiceClient();
    
      objCust.GetLocationsCompleted += new EventHandler<Demo1.DataService.GetLocationsCompletedEventArgs>(GetLocationsCompleted);
    
      objCust.GetLocationsAsync();
    
     }
    
    
    
     public void GetLocationsCompleted(object sender, Demo1.DataService.GetLocationsCompletedEventArgs e)
    
     {
    
      LocationDataCollection locationList = new LocationDataCollection();
    
      foreach (Demo1.DataService.Locations C in e.Result)
    
      {
    
      LocationData T = new LocationData();
    
      T.LocationID = C.LocationID;
    
      T.LocationSub = C.LocationSub;
    
      T.LocationName = C.LocationName;
    
      T.LocationState = C.LocationState;
    
      T.LocationStatus = C.LocationStatus;
    
      T.Location.Latitude = C.Latitude;
    
      T.Location.Longitude = C.Longitude;
    
      T.Notes = C.Notes;
    
      locationList.Add(T);
    
      }
    
    
    
      IEnumerator ppEnum = locationList.GetEnumerator();
    
      while (ppEnum.MoveNext())
    
      {
    
      this.Add((LocationData)ppEnum.Current);
    
      }
    
     }
    
     
    
     public class LocationsPushpin : Pushpin
    
     {
    
      //public string LocationsName { get; set; }
    
      //public string LocationsId { get; set; }
    
      //public string LocationsAddress { get; set; }
    
      public Int32 LocationID { get; set; }
    
      public String LocationSub { get; set; }
    
      public String LocationName { get; set; }
    
      public String LocationState { get; set; }
    
      public String LocationStatus { get; set; }
    
      public double Latitude { get; set; }
    
      public double Longitude { get; set; }
    
      public String Notes { get; set; }
    
      public LocationsPushpin(Color Bg)
    
      {
    
      this.Name = Guid.NewGuid().ToString();
    
      SolidColorBrush scb = new SolidColorBrush(Bg);
    
      this.Background = scb;
    
      }
    
     }
    
     }
    
    }
    
    
    
    

     



Alle Antworten

  • Donnerstag, 26. Mai 2011 23:02
    Moderator
     
     Beantwortet
    There are a copy of ways to go about it. One way is to only load the data for the state that is selected. Another idea is to separate the data for each state into it's own MapLayer. Another option is to add a property to your pushpins that has the state information and then create a method that loops through all pushpins in the map layer and changes the visibility of the pushpin based on the state information.
    Windows Live Developer MVP - http://rbrundritt.wordpress.com | http://inknowledge.co.uk
  • Dienstag, 31. Mai 2011 17:28
     
     

    Hi Richard, thanks for responding.

    I was thinking the same thing, and built a new StoredProcedure that adds a State paramter. How do I pass the Parameter from the XAML page to the DataService? It is not finding the control that  I added to the XAML.

     

    Thanks,