Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Dinesh's Blog
Browse by Tags · View All
brh 20
#SILVERLIGHT 19
#DOTNET 18
silverlight 18
#ASP.NET 15
Silverlight 4.0 9
bing 7
bing maps 6
wcf services 5
map 5

Archive · View All
August 2010 10
September 2010 3
July 2010 3
June 2010 3
October 2010 1

How to find Geographical Coordinates in Silverlight

Aug 5 2010 6:17PM by Dinesh Sodani   

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 Geocode Web Service Demo

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.

Bing Geocode 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:

  1. Source Code
  2. 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….

Tags: silverlight, bing, brh, Pushpin, Geospatial, #DOTNET, #SILVERLIGHT, bing map, #ASP.NET, Geographical Coordinates, Latitude and Longitude, find Latitude and Longitude,


Dinesh Sodani
30 · 6% · 1807
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

3  Comments  

  • Is there a way to pass a XML or a List Object to BING API so that i can pass all my addresses at once and in only one call i can get latitude & longitude for all the addresses.

    Thanks, Taher

    commented on Jul 27 2011 4:05AM

    -1 · -1% · -1
  • No. There is no such method available as far as I know.

    commented on Jul 27 2011 5:47AM
    Dinesh Sodani
    30 · 6% · 1807
  • PlatformServices.GeocodeResponse response = geocodeClient.Geocode(request);

    Error : 'SilverlightApplication2.PlatformServices.GeocodeServiceClient' does not contain a definition for 'Geocode' and no extension method 'Geocode' accepting a first argument of type 'SilverlightApplication2.PlatformServices.GeocodeServiceClient' could be found (are you missing a using directive or an assembly reference?)

    For above code it gives the error. can you help ??

    commented on Jun 1 2012 9:42AM
    masoodhussaini
    3015 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]