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
-
http://rt.se/49Z dewalt table saw
-
http://downtownsandiegochiro.com/ san diego chiropractor
-
http://www.muscleandfitnesslife.com muscle and fitness
-
http://www.kwloi.gr/ κώλοι
-
http://BringingTheFresh.com make money from home
-
http://www.familydometentreviews.com/ family dome tent
-
http://www.madpools.co.uk/ above ground swimming pools
-
http://www.samsung1080phdtv.net/ samsung 1080p hdtv
-
http://article.webmafia.in/business/buy-hip-hop-beats-that-successfully-reflects-the-sentiments-of-your-lyrics/ hip-hop beats
-
http://djtei1013.blogspot.com/ DJ TEI1013
-
http://www.seo-services-company.co.cc SEO Services
-
http://omega-3-fattyacids.com Omega 3 fatty acid
-
http://www.startmakingbeats.com/ Make Beats Like The Pros In Minutes !
- said: can you send me your code snippets on parvesh at i...
- tusto said: I already set publish_action and publish_stream an...
- said: please email me on parvesh at impact-works.com...
- Thusitha said: Thanks, No problem, I can send our details. Would ...
- said: yes you may use this, do send me your website link...
- said: can you send me screenshot or some other info on e...
- Posting to Facebook Wall in ASP.NET/C# using Graph API–Part 5
- Importing User info using facebook Graph API - Part 1
- Reading Facebook user's Wall with ASP.NET / C# using Facebook Graph API - Part 3
- Updating / Editing Facebook Event via Graph API using ASP.NET / C# – Part 7
- Creating Event(s) on Facebook with ASP.NET / C# using Graph API–Part 4






