EziData Solutions

Web, NET and SQL Server

Using Business Objects with the WP7 Map Control

One aspect of creating and adding pushpins to the Windows Phone 7 Map control that turned out to be a welcome revelation to me is that most geo-centric Business Objects can be used as the data source for the pushpin layer with little or no modification.

In a previous post we looked at how easy it was to add a single pushpin to the Map control. If we look at that method again, we see that all we really need is a geographical location. This means that as long as your object has a Latitude and Longitude, you can use it as a pushpin.

This was very handy in TrafficMATE especially when displaying camera locations on a map. The Camera object model we created already contained the Lat/Long of the camera. This meant that I could easily use a List<Camera> object as the ItemSource for the pushpin layer.

Here’s the xaml for a map showing camera locations.

<my:Map x:Name="CameraMap"

    CredentialsProvider="{Binding CredentialsProvider}"

    CopyrightVisibility="Collapsed"

    LogoVisibility="Collapsed"

    ZoomLevel="16">

    <my:Map.Foreground>

        <SolidColorBrush Color="{StaticResource PhoneForegroundColor}"/>

    </my:Map.Foreground>

    <my:MapItemsControl x:Name="PushpinItems" Height="300">

        <my:MapItemsControl.ItemTemplate>

            <DataTemplate>

                <my:Pushpin Location="{Binding Location}" >

                </my:Pushpin>

            </DataTemplate>

        </my:MapItemsControl.ItemTemplate>

    </my:MapItemsControl>

</my:Map>

The code that is of interest to us is the section defining the MapItemsContol named PushpinItems. Just like the other WP7 databindable controls, the MapItemsControl supports an ItemTemplate, which in this example is made up of Pushpin controls.

The Location property of the Pushpin, which is of type GeoCoordinate, is bound to a field in our data source called ‘Location’. All we need to support binding is ensure our business object exposes a GeoCoordinate property and we are good to go. A simplified version of the Camera object is shown below.

public class Camera

{

    public string Name { get; set; }

    public string Description { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

 

    public GeoCoordinate Location

    {

        get { return new GeoCoordinate(this.Latitude, this.Longitude); }

    }

}

With the help of an async call to a webservice, I populated a List<Camera> with relevant results and was able to set the ItemSource for the pushpin layer using something like:

PushpinItems.ItemsSource = cameras;

The key benefit of binding your objects directly to the pushpin layer is that you have direct access to the object whenever you select a pushpin. There is no need to make a second trip to the webservice when the user selects a pushpin - you already know everything there is to know about the underlying object. In a future post, I will explore how you can retrieve the underlying information from a Pushpin created using your business objects.

Posted: Dec 22 2011, 05:09 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Windows Phone 7 | WP7