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
<?xmlversion="1.0"encoding="UTF-8"?>
-<ResultSetversion="1.0">
    <!-- gws13.maps.sg1.yahoo.com uncompressed/chunked Thu Mar 8 02:52:52 PST 2012 -->
 
    <!-- wws3.geotech.sg1.yahoo.com 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>
        <quality>40</quality>
        <latitude>19.076191</latitude>
        <longitude>72.875877</longitude>
        <offsetlat>19.076191</offsetlat>
        <offsetlon>72.875877</offsetlon>
        <radius>23300</radius>
        <name/>
        <line1/>
        <line2/>
        <line3>Mumbai, Maharashtra</line3>
        <line4>India</line4>
        <house/>
        <street/>
        <xstreet/>
        <unittype/>
        <unit/>
        <postal/>
        <neighborhood/>
        <city>Mumbai</city>
        <county/>
        <state>Maharashtra</state>
        <country>India</country>
        <countrycode>IN</countrycode>
        <statecode>MH</statecode>
        <countycode/>
        <uzip/>
        <hash/>
        <woeid>2295411</woeid>
        <woetype>7</woetype>
    </Result>
</ResultSet>
 

 
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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
        <asp:Button ID="btnFind" runat="server" Text="Find Location" OnClick="FindLocation" />
        <hr />
        Latitude:
        <asp:Label ID="lblLatitude" runat="server" Text=""></asp:Label><br />
        Longitude:
        <asp:Label ID="lblLongitude" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

 

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

 
VB.Net
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.
C#
protected void FindLocation(object sender, EventArgs e)
{
    string appId = "{ConsumerKey}";
    string url = string.Format("http://where.yahooapis.com/geocode?location={0}&appid={1}", Server.UrlEncode(txtLocation.Text), appId);
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    using (WebResponse response = request.GetResponse())
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(response.GetResponseStream());
            lblLatitude.Text = ds.Tables["Result"].Rows[0]["Latitude"].ToString();
            lblLongitude.Text = ds.Tables["Result"].Rows[0]["Longitude"].ToString();
        }
    }
}

 
VB.Net
Protected Sub FindLocation(sender As Object, e As EventArgs)
    Dim appId As String = "{ConsumerKey}"
    Dim url As String = String.Format("http://where.yahooapis.com/geocode?location={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()
           ds.ReadXml(response.GetResponseStream())
           lblLatitude.Text = ds.Tables("Result").Rows(0)("Latitude").ToString()
           lblLongitude.Text = ds.Tables("Result").Rows(0)("Longitude").ToString()
        End Using
    End Using
End Sub

 

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