In this article I will explain how to find geographical coordinates: Latitude and Longitude of an address in Silverlight application. So that address can be displayed on the Bing Map.
In last few articles I wrote about how to add multiple locations on the Bing map in Silverlight. In that we assumed that we have geographic coordinates for all the addresses.I think it’s time to get answer of question “How to find geographic coordinates: Latitude and Longitude of an address”. In this article I will demonstrate the same. You can see the demo here.

Bing provides a web service to get the geographic coordinates. Below is the URL of that web service
http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc
Let’s create a Silverlight application and add service reference of the above web service.
Let’s add a text box where user can enter the address and find the Geocode : Latitude and Longtitude and Bing Map to show the address on the map as well.
<Grid x:Name="LayoutRoot">
<Border MinHeight="70" Margin="30,30,0,0" >
<Grid Margin="2,2,2,2" >
<Grid.RowDefinitions>
<RowDefinition MinHeight="30" MaxHeight="30" />
<RowDefinition MinHeight="30" MaxHeight="30" />
<RowDefinition MinHeight="30" MaxHeight="30" />
<RowDefinition MinHeight="30" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.9*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Margin="0,3,3,3" Text="Address:"
Width="50" HorizontalAlignment="Left" ></TextBlock>
<TextBox x:Name="txtAddress" Width="500" Grid.Column="1"Grid.Row="0"
Margin="0,0,0,0" Height="25" HorizontalAlignment="Left" ></TextBox>
<Button Content="Find" Grid.Row="0" Grid.Column="1" Click="Button_Click"
Margin="550,0,0,0" Width="50" HorizontalAlignment="Left"/>
<TextBlock Grid.Column="0" Grid.Row="1" Margin="0,3,3,3" Text="Latitude:"
HorizontalAlignment="Left" ></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="1" Margin="0,3,3,3"
Text="" x:Name="txtLatitude" HorizontalAlignment="Left"></TextBlock>
<TextBlock Grid.Column="0" Grid.Row="2" Margin="0,3,3,3"
Text="Longitude:" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="2" Margin="0,3,3,3" Text=""
x:Name="txtLongitude" HorizontalAlignment="Left" ></TextBlock>
<m:Map x:Name="MyMap" CredentialsProvider="Your Key" VerticalAlignment="Top"
Mode="Road" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
Margin="0,0,0,0"LogoVisibility="Collapsed" >
<m:MapLayer x:Name="GeocodeLayer"/>
</m:Map>
</Grid>
</Border>
</Grid>
Following function will call the Geocode method of PlatformServices webservice and return the Latitude and Longtitude:
public void GetGeocode()
{
//Object of Geocode webservice
PlatformServices.GeocodeRequest request = new PlatformServices.GeocodeRequest();
// Don't raise exceptions.
request.ExecutionOptions = new PlatformServices.ExecutionOptions();
request.ExecutionOptions.SuppressFaults = true;
PlatformServices.Credentials t = new PlatformServices.Credentials();
//This is Credentials Key from the Bing Map
t.Token = "Your KEY ";
//request.Query = "721 Bedford Avenue,Apt 6,Brooklyn,NY,11206";
request.Query = txtAddress.Text;
request.Credentials = t;
PlatformServices.GeocodeServiceClient geocodeClient =
new PlatformServices.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geocodeClient.GeocodeCompleted +=
new EventHandler<BingMapGeocodeService.PlatformServices.GeocodeCompletedEventArgs>(client_FindGeocodeCompleted);
geocodeClient.GeocodeAsync(request);
}
private void client_FindGeocodeCompleted(object sender,PlatformServices.GeocodeCompletedEventArgs e)
{
// Callback when the geocode is finished.
PlatformServices.GeocodeResult result = null;
string outString;
try
{
if (e.Result.ResponseSummary.StatusCode != PlatformServices.ResponseStatusCode.Success)
{
MessageBox.Show("error geocoding ... status <" + e.Result.ResponseSummary.StatusCode.ToString() + ">");
}
else if (0 == e.Result.Results.Count)
{
MessageBox.Show("No results");
}
else
{
//Reponse From the geocode service can have multiple results
//show only the first result.
outString = e.Result.Results[0].DisplayName;
txtLatitude.Text= e.Result.Results[0].Locations[0].Latitude.ToString();
txtLongitude.Text=e.Result.Results[0].Locations[0].Longitude.ToString();
Location loc = new Location(e.Result.Results[0].Locations[0].Latitude,
e.Result.Results[0].Locations[0].Longitude);
//Set the current view of the map to that location.
MyMap.SetView(loc, 15);
Ellipse point = new Ellipse();
point.Width = 20;
point.Height = 20;
point.Fill = new SolidColorBrush(Colors.Purple);
point.Opacity = 0.65;
Location location = new Location(loc);
MapLayer.SetPosition(point, location);
MapLayer.SetPositionOrigin(point, PositionOrigin.Center);
ToolTipService.SetToolTip(point, e.Result.Results[0].DisplayName);
//Drawing a Point on the map to show the location.
GeocodeLayer.Children.Add(point);
}
}
catch
{
MessageBox.Show("Exception raised");
}
}
In the previous article I described how to show multiple addresses on the bing map. We can call this geocode service directly in that. But calling the service for each and every address will take time so application will be slower and also there is a limit for number of Geocode requests can be sent to Bing Map in a day with your key. Bing limits you 30000 transactions per day in the free developer account.
So you can create a console application or a windows service which send request for each address and create an xml and send that xml to store procedure to update Location data in the table. See the function below to call Geocode Service in Console application:
public static void Geocode()
{
PlatformServices.GeocodeRequest request = new PlatformServices.GeocodeRequest();
request.ExecutionOptions = new PlatformServices.ExecutionOptions();
request.ExecutionOptions.SuppressFaults = true;
PlatformServices.GeocodeServiceClient geocodeClient =
new PlatformServices.GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
PlatformServices.Credentials t = new PlatformServices.Credentials();
t.Token = "Your Key";
String Address = "721 Bedford Avenue,Apt 6,Brooklyn,NY,11206";
if (Address != null && Address != "")
{
request.Query = Address.ToString();
request.Credentials = t;
PlatformServices.GeocodeResponse response = geocodeClient.Geocode(request);
if (response.Results.Count() > 0)
{
if (response.Results[0].Locations.Count() > 0)
{
Console.WriteLine("Latitude:- "
+ response.Results[0].Locations[0].Latitude.ToString() );
Console.WriteLine("Longitude:- "
+ response.Results[0].Locations[0].Longitude.ToString());
}
}
}
}
Download:
- Source Code
- Console Application
In SQL Server 2008 a new data type is added called “GEOGRAPHY”. In the next article I will show how to update GEOGRAPHY field with this Geocode service and how to show the location specified in GEOGRAPHY field on the Bing Map….