Monday, September 27, 2010

Windows Phone 7 Quick Tip #9 – Updating the Geo Location of a UIElement on a Map Layer

If you are using the Silverlight Bing Map control for Windows Phone (or for the desktop for that matter) you have the ability to add a MapLayer where you can add you controls or images at specific latitudes and longitudes.  You can really get creative and do some awesome things with a MapLayer.

Prior to SWFL Code Comp this year if I added a UIElement and needed to move it, I removed it from the original MapLayer and then added it back in at the new Lat & Lon where it should be displayed.

if (_drawLayer.Children.Contains(_myImage))
    _drawLayer.Children.Remove(_myImage);

_drawLayer.AddChild(_myImage, position, PositionOrigin.Center);

Each time I did this I had to hold my nose ‘cause this had such a bad smell to it…there had to be a better way, but I was just too busy, lazy, no I guess actually too busy to do the required research and this sort-of worked and a lot of other stuff didn’t.

After my talk this last weekend, Christopher Bennage showed me the errors of my way and schooled me how to do this correctly.

After initially adding the UIElement to the MapLayer we can simply update the position with the following chunk of code.

_myImage.SetValue(MapLayer.PositionProperty, position);

Much cleaner eh?

-twb

No comments:

Post a Comment