Community blog feed

WPF and Virtual Eath, Part 2

Website
Blog
Mike Taulty
Posted
05 Oct 2008 at 16:53

Summary

Following on from here, I thought I'd add a little bit of pushpin support to my basic Virtual Earth control for WPF. Essentially, I added a new property to the control called PushPins and each one of those is just a tuple of; { Latitude, Longitude, Text } to be placed on the map. This means that fro

Post extract

Following on from here, I thought I'd add a little bit of pushpin support to my basic Virtual Earth control for WPF.

Essentially, I added a new property to the control called PushPins and each one of those is just a tuple of;

{ Latitude, Longitude, Text }

to be placed on the map.

This means that from XAML I can create a set of pushpins in a declarative way as in;

      <ve:VirtualEarth
        x:Name="veControl"
        Grid.Column="1"
        ZoomLevel="{Binding Path=Value, ElementName=slidey}"
        Latitude="{Binding Path=Text, ElementName=txtLat}"
        Longitude="{Binding Path=Text, ElementName=txtLon}"
        MapStyle="{Binding Path=Text, ElementName=cmbMapStyle}">
        <ve:VirtualEarth.PushPins>
          <ve:PushPin
            Latitude="47.6"
            Longitude="-122.33"
            Text="Seattle 1" />
          <ve:PushPin
            Latitude="47.7"
            Longitude="-122.33"
            Text="Seattle 2" />
          <ve:PushPin
            Latitude="47.8"
            Longitude="-122.33"
            Text="Seattle 3" />
        </ve:VirtualEarth.PushPins>

      </ve:VirtualEarth>

or I could databind them but either way in code if I want to manipulate that collection I can just do something like;

veControl.PushPins.Remove(p);

or;

   PushPin p = new PushPin()
        {
          Latitude = double.Parse(txtAddLat.Text),
          Longitude = double.Parse(txtAddLon.Text),
          Text = txtAddName.Text
        };
        veControl.PushPins.Add(p);

and that seems to work reasonably well. I also added a lookup routine which will asynchronously go and look up a postcode and bring it back as a Lat/Lon which means I can now write code such as;

veControl.BeginLookupPostCode(txtPostcode.Text, Callback, txtPostcode.Text);

and I'll get a callback;

   void Callback(IAsyncResult result)
    {
      try
      {
        PostCodeLookupResult postCode = veControl.EndLookupPostCode(result);

        txtAddLat.Text = postCode.Latitude.ToString();
        txtAddLon.Text = postCode.Longitude.ToString();
      }
      catch (Exception e)
      {
        MessageBox.Show(e.Message);
      }
    }

which contains the Lat/Lon of the post-code that I looked up. What I haven't done is to implement INotifyPropertyChanged on a PushPin and then move the pin on the map if you change a property - I'd currently have to remove and re-add any pushpin that changed.

I added some support for PushPins into my little test harness which now looks like;

image

and allows me to basically manipulate the new functionality.

Once again, I've put the code up here for download.

We'd love to hear what you think! Submit ideas or give us feedback