You are on page 1of 5

7,682,156 members and growing!

(31,574 online)
Email Password Sign in Join Remember me? Lost password?

Home Articles Questions & Answers Learning Zones Features Help! The Lounge Search
» Web Development » Ajax » General

Licence

GeoLocation using REST, AJAX, First Posted


Views
21 Dec 2005
77,872
See Also
More like this
and Yahoo! for use with Google Downloads
Bookmarked
284
64 times
More by this author

Maps
By Matthew Hazlett | 24 Oct 2006
VB ASP.NET Javascript XML Windows .NET Visual-Studio HTML Dev Ajax ,+

Implementing geolocation using REST, AJAX and Yahoo! for use with Google Maps.

Article Browse Code Stats Revisions 4.04 (10 votes) 16 Sponsored Links

Download demo project - 11.6 Kb


See Also...
Download source - 26.1 Kb

Online demo

Introduction
This was a project I did to explore the Google Maps API. Google Maps is a great JavaScript The Daily Insider
driven service, and extremely easy to use and manipulate (Great job, Google). But, it has
a severe limitation, it does not have a source of geo-location information accessible via the
API.

For example, if I want to display my address on a Google Map, I need to know the latitude
and longitude so I can set the point via the API. This presented a problem as I have no
idea what that would be, and in a real world application, if I wanted to plot some data
points, I would need a way figure that out.

Yahoo! to the rescue! Yahoo! has a very cool (and free) Web Service for GeoCoding.
However, the trick is to make Yahoo!'s service play nice with Google Maps. The answer was
obvious, I needed to use AJAX and a custom class to query Yahoo!'s information directly
and transfer it to the JavaScript.

Geocode Class Internals


The Geocode class I wrote to query Yahoo!'s Web Service is very simple. All I had to do
was construct the URL according to the specifications defined by Yahoo!, and point an
XmlDocument to that URL. Next, I parse the data using xPath statements to a
Hashtable. (I'm not an XML guru, there is most likely a better way to write these xPath
queries.)

Collapse
Private Function Query
Dim XmlDoc as new XmlDocument
Dim XmlNS as new XmlNamespaceManager(xmlDoc.NameTable)

Try
XmlDoc.Load(BuildURL)
XmlNS.AddNamespace("def","urn:yahoo:maps")

mResults("Latitude") = _
XmlDoc.SelectSingleNode(".//def:Latitude", XmlNS).InnerXML
mResults("Longitude") = _
XmlDoc.SelectSingleNode(".//def:Longitude", XmlNS).InnerXML
mResults("Address") = _
XmlDoc.SelectSingleNode(".//def:Address", XmlNS).InnerXML
mResults("City") = _
XmlDoc.SelectSingleNode(".//def:City", XmlNS).InnerXML
mResults("State") = _
XmlDoc.SelectSingleNode(".//def:State", XmlNS).InnerXML
mResults("Zip") = _
XmlDoc.SelectSingleNode(".//def:Zip", XmlNS).InnerXML
mResults("Country") = _
XmlDoc.SelectSingleNode(".//def:Country", XmlNS).InnerXML
Catch e as Exception
Return False
End Try

Return True
End Function

Note: For full details, please see the source code, and for information on how I constructed
the URLs, refer to my URLBuilder class (included in the source).

GeoCoder Class In Action


To query Yahoo!'s Web Service, all you need to do is create the object and pass it two
parameters:

Collapse

Dim myGeocode as New Clarity.Utils.Geocode("AppID", "Free Form Text")

Alternately, you can also pass them in like this:

Collapse

Dim myGeocode as New Clarity.Utils.Geocode("AppID")

myGeocode.Street = ""
myGeocode.City = ""
myGeocode.State = ""
myGeocode.Zip = ""
myGeocode.Location = ""

What is an ApplicationID?
Register with Yahoo! for your free ApplicationID.

Now, to read the data sent back from the query is extremely easy. Just execute the
Results function passing in the corresponding values.
Collapse
'*****************************************

' Here are the possible values:

'*****************************************

' Clarity.Utils.Geocode.ResultType.Latitude

' Clarity.Utils.Geocode.ResultType.Longitude

' Clarity.Utils.Geocode.ResultType.Address

' Clarity.Utils.Geocode.ResultType.City

' Clarity.Utils.Geocode.ResultType.State

' Clarity.Utils.Geocode.ResultType.Zip

' Clarity.Utils.Geocode.ResultType.Country

'

Console.WriteLine(" Latitude: " & _


myGeocode.Results(Clarity.Utils.Geocode.ResultType.Latitude))
Console.WriteLine("Longitude: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.Longitude))

Geocode Class Testing Application


Not much to explain with this, I just needed a way to test the GeoCode class, so I wrote a
quick console app.

Collapse

Sub Main()
Dim Location as String

Console.WriteLine(".NET Interface" & _


" for Yahoo's GeoLocation Web-Service")
Console.WriteLine("Matthew Hazlett, Clarity Computers")
Console.WriteLine()

Console.Write("Enter address: ")


Location = Console.ReadLine
Console.WriteLine()

Dim myGeocode as New Clarity.Utils.Geocode("AppID", Location)

Console.WriteLine(" Address: " & _


myGeocode.Results(Clarity.Utils.Geocode.ResultType.Address))
Console.WriteLine(" City: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.City))
Console.WriteLine(" State: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.State))
Console.WriteLine(" Zip: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.Zip))
Console.WriteLine(" Country: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.Country))
Console.WriteLine()
Console.WriteLine(" Latitude: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.Latitude))
Console.WriteLine("Longitude: " & _
myGeocode.Results(Clarity.Utils.Geocode.ResultType.Longitude))
Console.WriteLine()

Console.Write("...Slam Return...")
Console.Read
End Sub

AJAX vs. Mr. Clean


If you don't know what AJAX is then you can refer to the plethora of information on the
web. [Definition, AJAX Library for .NET]

I'm not going to go into how to add AJAX to your project or how to create an AJAX page.
You will need to refer to the Quick Start documentation for that information. I have also
not included the Google Mapping functions, this is just a sample application of how to query
Yahoo! with AJAX (but they are included in the source).

The following is the code used by the web application to accomplish the GeoCode
functions:

Collapse
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e AsSystem.EventArgs) Handles Me.Load
Ajax.Utility.RegisterTypeForAjax(GetType(_Default))
End Sub

<Ajax.AjaxMethod()> _
Public Function Lookup(Location as String) as ArrayList
Dim Geo as new Clarity.Utils.Geocode("AppID", Location)
Dim Results as New ArrayList

Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.Latitude))

Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.Longitude))
Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.Address))
Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.City))
Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.State))
Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.Zip))
Results.Add(Geo.Results(Clarity.Utils.Geocode.ResultType.Country))

Return Results
End Function

The Lookup function looks like a normal function but it's really an AJAX function. This
means we can call this function from the JavaScript contained in the page (pretty neat).
Finally, here is the JavaScript used on the page to invoke the Lookup function.

Notice the function is called like this: Result = Class.Function(args);.

Collapse

function MapMe() {
text = document.getElementById("Location").value;
LocData = _Default.Lookup(text);

document.getElementById("Latitude").innerHTML = LocData.value[1];
document.getElementById("Longitude").innerHTML = LocData.value[0];
document.getElementById("Address").innerHTML = LocData.value[2];
document.getElementById("City").innerHTML = LocData.value[3];
document.getElementById("State").innerHTML = LocData.value[4];
document.getElementById("Zip").innerHTML = LocData.value[5];
document.getElementById("Country").innerHTML = LocData.value[6];
}

Finally!
Well, this wasn't the most complicated project but definitely one of the most fun! I wanted
to share my work and hope someone else will find it useful and create the next great
HousingMaps.Com (not mine, just a cool Map Hack).

Post up the links to your Google Map Hacks!

License
This article has no explicit license attached to it but may contain usage terms in the article
text or the download files themselves. If in doubt please contact the author via the
discussion board below.

A list of licenses authors might use can be found here

About the Author


Matthew Hazlett I started programming for fun when I was about 10 on an
Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the
years. More then I can say for my Microsoft Certifications.

The way I learned was by example, now its time to give back to
the next generation of coders.

Web Developer

United States

Member

Sign Up to vote for this article


Article
Top
Comments and Discussions
You must Sign In to use this message board. (secure sign-in)
FAQ Search

Noise Tolerance Medium Layout Normal Per page 25 Update

Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh) First Prev Next


Update Matthew Hazlett 8:34 9 Feb '10

GeoLocation adapted for ASP.NET solution? sbx14 9:52 1 May '07

Re: GeoLocation adapted for ASP.NET solution? tanweer akhtar 23:29 11 Aug '10

How to get Google Maps API Lord_Chiru 17:43 6 Jan '07

Alternative method (purchase data) Steven Berkovitz 7:43 24 Oct '06

I say free is a better price... Eli.M 7:37 1 Nov '06

Visual Studio 2003 Version djvoracious 16:51 4 May '06

Port this to pageflakes.com Omar Al Zabir 17:29 2 Jan '06

Re: Port this to pageflakes.com Matthew Hazlett 17:34 2 Jan '06

Title Brad Bruce 5:16 22 Dec '05

Live Demo Matthew Hazlett 11:34 21 Dec '05

Re: Live Demo Smitha Vijayan 11:41 21 Dec '05

Re: Live Demo Matthew Hazlett 11:46 21 Dec '05

Re: Live Demo Phebous 10:30 27 Dec '05

Re: Live Demo Jafin 19:21 23 Oct '06

Re: Live Demo Matthew Hazlett 19:29 23 Oct '06

Last Visit: 19:00 31 Dec '99 Last Update: 2:13 5 Apr '11 1

General News Question Answer Joke Rant Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

link | Privacy | Terms of Use | Mobile Copyright 2005 by Matthew Hazlett


Last Updated: 24 Oct 2006 Everything else Copyright © CodeProject, 1999-2011
Web21 | Advertise on the Code Project

You might also like