Using Yahoo placefinder API to fetch Longitude/Latitude

// July 2nd, 2011 // ASP.NET, C#

Have you ever been in a situation where to need longitude and latitude by place name, if yes the Yahoo Placefinder API is your friend, here i will demonstrate a small snippet of code which can be used to call placefinder api and use the resultant xml to get Longitude/Latitude.

First you need to setup an application at yahoo developers portal , register your application at the following link:

http://developer.yahoo.com/geo/placefinder/

 

click on button and register your app and note down your APPID. Once ready with APPID fire up Visual Studio / Visual Webdeveloper and start a new project.

 

For the sake of simplicity we will create a simple UI like below

 

Just a textbox and button and 2 labels for Longitude and Latitude.

To call the Yahoo Placefinder API we need to send a GET request to the http://where.yahooapis.com/geocode , we also need to send following parameters:

q : search query , it can be a place name or Long/Lat for reverse lookup

appid: your registerd appid

for complete list of parameters check http://developer.yahoo.com/geo/placefinder/

Yahoo Placefinder emits the following XML in response (click on image to enlarge):

if you look closely the response contains Longitude/Latitude and that is what we are looking for, parsing XML is a trivial and error prone task so we will use XML to Objects way to convert the xml to a C# Classes

to know more on how to convert XML to C# Objects please read on by clicking here

lets setup C# Classes for response xml , here is the code:

[Serializable]
[XmlRoot(ElementName="ResultSet",Namespace="")]
public class GeoCoordinates
{
[XmlElement(ElementName="Result")]
public Result result{get;set;}
}

public class Result
{
[XmlElement(ElementName = "longitude")]
public string Longitude { get; set; }
[XmlElement(ElementName = "latitude")]
public string Latitude { get; set; }
}

Now on the button click write this code , please replace “YOUR APP ID” with appid

protected void btnFind_Click(object sender, EventArgs e)
{
//API Url

string Url = "http://where.yahooapis.com/geocode?q={0}&appid={1}";
//Formatter url with appid and search query
string formattedUrl = string.Format(Url, HttpUtility.UrlEncode(txtPlacename.Text),"your app id");

//create a new httprequest
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(formattedUrl);

//Get the response
var response = request.GetResponse();

//Serializing Response XML to C# Class
XmlSerializer sr = new XmlSerializer(typeof(GeoCoordinates));
var result = sr.Deserialize(response.GetResponseStream()) as GeoCoordinates;

lblLatutude.Text = result.result.Latitude;
lblLongitude.Text = result.result.Longitude;

response.Close();
}

The code is very simple and straightforward , start your project, put in place name in textbox and click Find Now and you should see something like this:

 

  • http://wrhel.com/top-recommended-sites/ Wrhel Consulting and Design

    Wrhel Consulting and Design…

    […] … great article that everyone should read […]…

  • http://rt.se/49Z dewalt table saw

    Great information…

    Great information mazon.com amzon.com amazn.com amazon.cm…

  • http://downtownsandiegochiro.com/ san diego chiropractor

    Great information…

    This is distinctive. American checked out the plant video content so we are confounded. We’re fascinated by one of these everything. Person appreciate your quotation, and prize doing in this. Please keep add relevant content. These are uncommonly nece…

  • http://www.muscleandfitnesslife.com muscle and fitness

    Great information…

    This can be amazing. Only looked at every single levels so we are astounded. We are most certainly attracted to one of these behaviors. Persons appreciate more instruction, and significance the effort inside this. Please keep editing. These are surpris…

  • http://www.kwloi.gr/ κώλοι

    Great information…

    This is extraordinary. American watch this unique subject matter when we are astonished. We’re interested in this kind of qualities. Some individuals appreciate your upcoming involvement, and regard your precious time with this. Please keep editing. T…

  • http://BringingTheFresh.com make money from home

    Great information…

    This can be marvelous. Issue checked out the herb gratification when we are stupefied. We are precisely interested in one of these things. Experts appreciate your prospective gather, and estimate your precious time while in this. Please keep enhancing….

  • http://www.familydometentreviews.com/ family dome tent

    Great information…

    This can be astounding. One particular watch every group gist when we are confounded. We’re attracted to this type of problems. A good appreciate slimmer s time, and number doing while in this. Please keep editing. They are particularly cherished fact…

  • http://www.madpools.co.uk/ above ground swimming pools

    Great information…

    This is often brilliant. The stare upon this in turn content material material material when we are greatly surprised. We are precisely fascinated by this kind of areas. We appreciate each of the counsel, and assess your precious time in this. Please k…

  • http://www.samsung1080phdtv.net/ samsung 1080p hdtv

    samsung 1080p hdtv…

    More and more writings just have words in them that mean little to nothing. You have written an article that is very deep in meaning and kept it simple for the average reader to understand. http://www.samsung1080phdtv.net/

  • http://article.webmafia.in/business/buy-hip-hop-beats-that-successfully-reflects-the-sentiments-of-your-lyrics/ hip-hop beats

    Great information…

    This is certainly significant. Individuals checked out this key fact keep so we are impressed. We are interested in this sort of organisms. Another one appreciate slimmer s participation, and enjoy the effort inside this. Please keep control. They are …

  • http://djtei1013.blogspot.com/ DJ TEI1013

    Great information…

    This can be superior. Nutritionists looked at this gift video material and we are impressed. We are most certainly curious about one of these problems. Persons appreciate users grocery store, and assess the effort with this. Please keep control. They’…

  • http://www.seo-services-company.co.cc SEO Services

    Find Out More…

    [...]always a big fan of linking to bloggers that I love but do not get a good deal of link enjoy from[...]…

  • http://omega-3-fattyacids.com Omega 3 fatty acid

    Get the facts…

    [...]You are so awesome! I do not believe I’ve truly read through something like this before. So great to find another person with a few original thoughts on this subject matter. Seriously.. thanks for starting this up. This site is something that is …

  • http://www.startmakingbeats.com/ Make Beats Like The Pros In Minutes !

    Great information…

    This can be first-class. First watch this approach written when we are astonished. We are precisely fascinated by this type of possessions. Human beings appreciate human recommendations, and benefit your precious time inside this. Please keep add relev…