In this article I will explain how to find Latitude and Longitude for a location based on its Address using Yahoo Location API of the Yahoo Developer Network (YDN).

Registering and getting Consumer Key
The first thing you need to do is that register and get the Consumer (API) key using the following link
Note: You need to register only when you want to deploy your application on server. For local testing you don’t require Consumer (API) key.
After registration you can find your registered applications at the following

Yahoo Location API Response XML
In order to check the structural format of the returned XML response from Yahoo Location API, you can test it by putting the following URL in browser
And it will return the following output
    <!-- uncompressed/chunked Thu Mar 8 02:52:52 PST 2012 -->
    <!-- uncompressed/chunked Thu Mar 8 02:52:52 PST 2012 -->
    <Error>0</Error><ErrorMessage>No error</ErrorMessage><Locale>us_US</Locale><Quality>40</Quality><Found>1</Found>-<Result>
        <line3>Mumbai, Maharashtra</line3>

HTML Markup
Below is the HTML Markup of the page, I have an ASP.Net TextBox where user can enter the location for which he wants to find the latitude and longitude, an ASP.Net Button which finds the find the latitude and longitude which are later displayed in the two ASP.Net Label controls.
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
        <asp:Button ID="btnFind" runat="server" Text="Find Location" OnClick="FindLocation" />
        <hr />
        <asp:Label ID="lblLatitude" runat="server" Text=""></asp:Label><br />
        <asp:Label ID="lblLongitude" runat="server" Text=""></asp:Label>


You will need to import the following namespaces
using System.Net;
using System.Data;

Imports System.Net
Imports System.Data


Using the Yahoo Location API to find Latitude and Longitude
When the ASP.Net Button is clicked the following event handler is triggered which sends a Web Request to the Yahoo Location API which returns the XML response. The XML response is then read into a DataSet and then finally the Latitude and Longitude information is displayed in their respective Label controls.
protected void FindLocation(object sender, EventArgs e)
    string appId = "{ConsumerKey}";
    string url = string.Format("{0}&appid={1}", Server.UrlEncode(txtLocation.Text), appId);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    using (WebResponse response = request.GetResponse())
        using (DataSet ds = new DataSet())
            lblLatitude.Text = ds.Tables["Result"].Rows[0]["Latitude"].ToString();
            lblLongitude.Text = ds.Tables["Result"].Rows[0]["Longitude"].ToString();

Protected Sub FindLocation(sender As Object, e As EventArgs)
    Dim appId As String = "{ConsumerKey}"
    Dim url As String = String.Format("{0}&appid={1}", Server.UrlEncode(txtLocation.Text), appId)
    Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    Using response As WebResponse = request.GetResponse()
        Using ds As New DataSet()
           lblLatitude.Text = ds.Tables("Result").Rows(0)("Latitude").ToString()
           lblLongitude.Text = ds.Tables("Result").Rows(0)("Longitude").ToString()
        End Using
    End Using
End Sub


You can download the complete code in C# and VB.Net using the download link provided below