Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Morning All,

I have a Map application that contains multiple PushPins that are populated by a DataService. I have been able to display the PushPins on the map.

Now what I am trying to do is set the visibility of the PushPins selectively. I want to do a Show All, and then show only the specific States that I want (California, Oregon, etc).

What do I need to put in the Code behind to do this?

Thanks,

Here is the source for the project. The attached file is the Database I am using in CSV Format, the Table Structure, and the Stored Procedures.

SQL Files.zip
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="Ak0QYnxJb69SewXWdXHU5esYGd_wKp47uozov34pmduqApyRYaa4TVl1Z7TU_r3R" 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>

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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace Demo1.Web
{
  [DataContract]
  public class Locations
  {
    [DataMember]
    public Int32 LocationID { get; set; }

    [DataMember]
    public String LocationSub { get; set; }

    [DataMember]
    public String LocationName { get; set; }

    [DataMember]
    public String LocationState { get; set; }

    [DataMember]
    public String LocationStatus { get; set; }

    [DataMember]
    public double Latitude { get; set; }

    [DataMember]
    public double Longitude { get; set; }

    [DataMember]
    public String Notes { get; set; }
  }
}

using System.Collections.Generic;
using System.ServiceModel;
using Demo1.Web;

namespace Demo1.Web
{
  [ServiceContract]
  public interface IDataService
  {
    [OperationContract]
    List<Locations> GetLocations();
  }
}
Posted
Updated 27-May-11 22:43pm
v2
Comments
Mark Salsbery 28-May-11 12:30pm    
Can you bind the Visibility property of the Pushpins using a custom IValueConverter that sets the property based on your criteria?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900