Thursday, December 15, 2011

Using Silverlight Bing Map Controls

 

Referred URL

http://cm-bloggers.blogspot.com/2010/01/first-steps-with-silverlight-bing-maps.html

-> To use the Silverlight Bing Maps control, you need to sign up at the Bing Maps Account Center and obtain a key.

-> Download and install the Bing Maps control.

-> Then create a new Silverlight application and add a reference to the assemblies provided with the control as shown here:

XAML File

<UserControl x:Class="BingMaps.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"

mc:Ignorable="d"

d:DesignHeight="750" d:DesignWidth="700" Width="Auto" Height="Auto">

<m:Map Name="map" CredentialsProvider="Place your Bing Key" Width="700" Height="700" />

</UserControl>

CS File

private void b1_Click(object sender, RoutedEventArgs e)

        {

Uri url = new Uri("../Feed.xml", UriKind.Relative);

WebClient client = new WebClient();

            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            client.DownloadStringAsync(url);

        }

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

if (e.Error == null)

            {

//Reads XML and Populates the Pushpins in Map

StringReader stream = new StringReader(e.Result);

XmlReader reader = XmlReader.Create(stream);

                reader.MoveToContent();

while (reader.Read())

                {

if (reader.NodeType == XmlNodeType.Element && reader.Name == "entry")

                    {

//Adds Tooltip

var tooltipText = "Unkown";

                        tooltipText = reader.GetAttribute("title");

                        tooltipText = tooltipText + Environment.NewLine + reader.GetAttribute("description");

//Adds Pushpin pertaining to Longitude and Latitude

string[] loc = reader.ReadInnerXml().Split(" ".ToCharArray());

double lat = Double.Parse(loc[0]);

double lon = double.Parse(loc[1]);

Pushpin p = new Pushpin();

                        p.Location = new Location(lat, lon);

                        tooltipText = tooltipText + Environment.NewLine + "Latitude - " + lat + Environment.NewLine +"Longitude - " + lon;

                        System.Windows.Controls.ToolTipService.SetToolTip(p, tooltipText);

                        p.Cursor = Cursors.Hand;

                        map.Children.Add(p);

                    }

if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "entries")

                    {

break;

                    }

                }

                reader.Close();

            }

        }

Note - If map is not showing and throwing ‘Unable to connect server’ error. In Internet options check the proxy settings and enable‘Automatically detect settings’

No comments: