Shalvin Interests

Wednesday, March 23, 2011

Bing Map Silverlight Control Dynamically Adding Pushpins

Silverlight Bing Map controls enables you to have Map functionality in your Silverlight application. Once you install Bing Maps Silverlight Control you will get a directory name Bing Maps Silverlight Controls under Program Files. Inside V1, Libraries you can find the associated dlls.

Add a reference to both the controls.
Add a namespace to Page section pointing to the dll.

  xmlns:Map="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"

 Now you can use the control

 <Map:Map Name="map1">
    <Map:Pushpin Location="9,76" Content="Kochi"/>
 </Map:Map>

Creating dynamic Pushpins
using Microsoft.Maps.MapControl;

Shalvin.Service1Client sc = new Shalvin.Service1Client();
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
           
            sc.getlocationCompleted += new EventHandler<Shalvin.getlocationCompletedEventArgs>(sc_getlocationCompleted);
            sc.getlocationAsync();
        }

        void sc_getlocationCompleted(object sender, Shalvin.getlocationCompletedEventArgs e)
        {
            var locs = e.Result.ToList();
            Pushpin p;
            Location l;
            foreach (var g in locs)
            {
                p = new Pushpin();
                double la = double.Parse(g.Latitude.ToString());
                double lo = double.Parse(g.Longitude.ToString());
                l = new Location { Latitude = la, Longitude = lo };
                p.Content = g.LocationName;
                p.Location = l;
                map1.Children.Add(p);
            }
        }
    }