About | Contact | Register | Advertise | FAQ
Free GISuser Newsletter
HomeNewsArticlesDataJobsEDUCommunityGalleryForumsLBSzoneSTOREBlogFlickr
Software | Spotlights on Geospatial Data | GIS Education / Events | Hardware | Mobile | Web Services | Earth Imagery  
advertisement

GISuser Newsletter

GIS & LBS News - 3X A Week
View recent edition

newsletter
 
Get the popular GISuser Today Newsletter SUBSCRIBE HERE
Register as a GISuser!

RSS Get GISuser via RSS


GISuser Sponsor


Featured Contest

 Participate in LizardTech's Contest at ESRI and Win a GPS!!

GISuser Sponsor


Recent Site Additions
Locago, free mobile map browser, now available to the public with an open API
Spatial Media LBSzone Alert - Adapx, Nokia, Apple making LBS news
OGC Elects Two New Directors; Lisa Campbell, VP Autodesk, Dr. John C. Curlander Microsoft Boulder
Sidwell to Sponsor Cadastral/Land Records SIG Meeting at 2008 ESRI International User Conference
Intergraph Enhances Electric Utility and Co-op Offerings with Work Management Solution
PCI Geomatics Becomes OGC Principal-Plus Member
GITA Event - New Two-day ROI Workshop Set for Aug. 7-8
Yotta’s mobile laser scanner maps Southampton’s roads
ESRI to Host GIS Pavilion at American Public Works Association Congress and Expo
RMSI Shares Success Stories at ESRI User Conference, San Diego
3001 Awarded JALBTCX U.S. Army Corps of Engineers Surveying and Mapping Services Contract


GISuser Events
July 2008
MTWTFSS
30
1
2
3

GISuser Sponsor


GISuser Web 2.0

GISuser Sponsor


Home arrow Articles arrow Web Services arrow A Look at the Geocoding Functionality Build Into Google Maps API V2     

GISuser HOT JOBS!
MORE COOL STUFF! GISuser Today , the Flickr & the AnyGeo Blog, Map Gallery

A Look at the Geocoding Functionality Build Into Google Maps API V2   PDF  Print  E-mail
Written by Eric Pimpler  
Tuesday, 27 June 2006
When the Google Maps API was released in early 2005, one of the most notable omissions was the ability to geocode addresses.  This posed more of an inconvenience that anything else as there are many geocoding API’s available from various sources, some of which are free to use.  Earlier this month that all changed as Google added geocoding functionality to it’s Maps API... read on about it in this fine contribution.

When the Google Maps API was released in early 2005, one of the notable omissions was the ability to geocode addresses.  This posed more of an inconvenience that anything else as there are many geocoding API’s available from various sources, some of which are free to use.  In addition, clever application developers quickly found a Google Maps hack that would simulate geocoding functionality through Google Maps.  However, the lack of true geocoding functionality built into the API was a bit of hindrance.  Earlier this month that all changed as Google added geocoding functionality to it’s Maps API in the form of a GClientGeocoder object which allows you to submit addresses for geocoding via JavaScript.  This functionality can also be accessed via HTTP requests directly from a client browser.  Some nice features of the geocoding functionality include:

- No need to break up the address into street components such as street name, city, country.  The address can be submitted as a single string.
- No need to worry about capitalization or punctuation
- Geocoder returns a nicely formatted version of the address you sent
- Geocoder returns the address broken up into components like street, country, province, prefecture, postal code, etc.
- Simpler version that returns a GLatLng
- Built-in cache to make the user experience faster response times on commonly used addresses
- Street level geocoding for the U.S., Canada, France, Germany, Italy, Spain, and Japan

At this time you are limited to 50,000 geocode requests per day per API key.  According to Google if you have a fairly stable database of addresses (e.g. a list of properties for sale), it is recommended that you geocode them once using the HTTP request method and cache the coordinates in your own database. This means your site will be faster for your users and also uses up less of your daily quota of geocode requests. 

GClientGeocoder
Let’s focus on the functionality provided by the GClientGeocoder object. 

An instance of GClientGeocoder can be created with the following line of code:

This instance of a geocoder talks directly to Google servers to fulfill geocoding requests.  An optional cache parameter allows you to specify a custom client-side cache of known addresses.  GClientGeocoder has two methods that can be used to send an address to Google for geocoding: getLatLng( ) and getLocations( ).

The getLatLng( ) method sends a request to the Google servers to geocode the specified address.  If the address is successfully located, the user-specified callback function is invoked with a GLatLng point. Otherwise, the callback function is given a null point. In the case of an ambiguous address, only the point for the best match is passed to the callback function.  Take a look at the code example below for more information on how to call getlatLng( ).  In this example, we geocode an address, add a marker at that point, and open an info window displaying the address.

The getLocations( ) method also sends a request to Google to geocode a specified address.  However, getLocations( ) differs in that it returns a JSON object containing a status code and one or more Placemark objects.  The status code is a response indicating whether the geocode request was successful or not.  Here is a full listing of the status codes obtained from the Google documentation:

In addition to the status code, one or more Placemark objects are returned.  Multiple Placemark objects are returned in the event that the geocoder finds more than one match.  Each Placemark object is composed of an address (nicely formatted and capitalized), AddressDetails, and a Point representing the location of the geocoded address.  For an example of a typical JSON object returned by the getLocations( ) method as well as a code example please see the Google documentation.
Caching Geocodes with GGeocodeCache
Performance of the geocoder can be improved through the use of the caching functionality built into GClientGeocoder.  This cache stores geocoded responses so that if the same address is geocoded again, the response will be returned from the cache rather than the Google geocoder.  This improves the performance of your application and lessens the number of requests sent to the geocoding service.  By default, caching is enabled, but can be turned off by passing a null value to the setCache( ) method on GClientGeocoder.  Caching is controlled through the GGeocodeCache class.  A new instance of GGeocodeCache is created through the GGeocodeCache( ) constructor which immediately call the reset( ) method to empty the cache.  Addresses can then be placed into the cache with the put( address,reply) method which stores the given reply under the given address.  Addresses can be retrieved from the cache through the get(address) method which returns the reply stored under the given address.  As we mentioned, the reset( ) method purges all addresses from the current cache.

One of the useful aspects of caching functionality is the ability to pre-build a cache to account for commonly used addresses in an application.  For example, if you have an application that displays common tourist attractions you would want to pre-build a client cache containing the geocoded address for each of the attractions.  This would remove the need to continually query the Google geocoder for the point of interest.
HTTP Geocoding Requests
In addition to the GClientGeocoder object you can also access the Maps API geocoder functionality through HTTP requests.  Combined with the XmlHttpRequest AJAX object, this gives you the ability to send geocode requests through server-side scripting.  A request should be sent to http://maps.google.com/maps/geo? with the following parameters:
- q - The address that you want to geocode
- key – Your Google Maps API key
- output – The format option (xml, kml, json)

For example:

http://maps.google.com/maps/geo?q=21734+Longwood,+San+Antonio+TX&output=xml&key=ABQIAAAA7_kD1t_m22HBF9feCaDPZxQZuc26M5nLyzIhAY0gIOH-LGKPdxQ6r3IzloZck1JnK6eAB02QGYg4Tg
In this case, we have specified a return type of “xml” which returns an XML output.  Open a web browser and paste the code above into the address bar and hit the “Enter” key to see the XML output returned by an HTTP request to the geocoder.  It should look something like what you see in the figure below.

google maps geocoder

Conclusion

According to Google, geocoding functionality was the most requested feature by developers.  This should come as no surprise since most applications built with the Google Maps API rely on placing points of interest on a map.  The recent update of the API to include geocoding functionality is a big step forward in the evolution of this already popular web mapping toolkit. 

Author Information

Mr. Pimpler is the owner of Geospatial Training Services, LLC, a provider of virtual and instructor led GIS training opportunities and the author of its popular virtual training course “Google Maps For Your Apps!” and the new PDF book “Google Maps API: The New World of Web Mapping”.  For more information on the Google Maps API or any other training opportunity provided by GeoSpatial Training Services, please visit our website at http://www.geospatialtraining.com

Looking for more gmaps tutorials? See Also:

  •  

    Number of comments (0) - Add your comments to this article...


    Digg!

    Share This Item 

    del.icio.us / Furl / digg this item!Digg / Slashdot / Y!MyWeb / reddit / newsvine  addtoany
    Share on Facebook

    Get the GISuser Today Newsletter (3X a week!)
     


    The Editor's Blog


    Glenn's AnyGeo BlogSee more threads and details about Glenn's AnyGeo Weblog HERE The Editor (Glenn) started the AnyGeo blog some time ago and the threads are now also mirrored here at GISuser.com - RSS feed is available to add to your favorite news reader.

    Featured Events
    • GeoWeb 2008 - This exciting annual conference will take place in Vancouver, Canada from July 21-25th, 2008 at the Morris J. Wosk Centre for Dialogue. The GeoWeb 2008 conference welcomes both public and private organizations to meet, discuss and learn about today’s most innovative geospatial technologies.
    • 2008 ESRI International User Conference (ESRI UC) - Users from more than 120 countries come to learn new skills, share information, and discover best practices, tips, and tricks that they can use instantly. Be part of this extraordinary experience August 4–8, 2008, in San Diego, California.
    • 2008 ESRI Survey & Engineering GIS Summit - August 2-5, San Diego, California. Join more than 400 surveyors and engineers in exploring the possibilities of GIS technology. See how GIS software integrates with surveying and engineering tools to provide more complete business solutions and field processes.
    • GITA 17th Annual GIS for Oil & Gas Conference and Exhibition, set for Sept. 21-24, 2008 - The conference is the only event of its kind, devoted exclusively to geospatial applications and technologies for all aspects of the oil and gas industry.

    List Your Event Here


    Recent GISuser Discussions
    1: Arcpad Class by DebbyB
    2: Geospatial Professional (3 Yrs Exp) by Shane Smith
    3: Apple Iphone 16GB/ New Edition 3G by telcom
    4: Cartographer / GIS Analyst by Nicholas Beltramelli
    5: Cartographer / GIS Analyst by Nicholas Beltramelli

    show last 4hrs - 24hrs

    Google Geospatial Search
    Google
     

     

    or... try our CUSTOM GISuser Google Search!

    Contribute to the GISuser Search (by Google)


    Today's Top News


    Sponsor




    GISuser RSS Feed
    Get the latest Geospatial news
    direct to your desktop
    RSS


    feedburner
    add to google reader




    technocrati

    See ALL the GISuser Feeds


    GISuser Site Sponsor


    Most Popular
    Gmaps 101 - An Introduction to Google Maps & The Google Maps API (Part 1)
    The GISuser's Guide to locating and downloading Free USGS data
    Hackers Tap Into the Functionality and Simplicity of Google Maps
    GISuser Guide to downloading Free 7.5 minute DEMs
    Gmaps 101 - An Introduction to Google Maps & The Google Maps API V2 (Part 2)
    BearingPoint, ESRI SAS Introduce Leading-Edge Development Planning Solution for Commercial Retailers
    GIS Community Resources
    State GIS Clearinghouse Directory - Update, July 2004
    ShakeMap — A Tool for Earthquake Response
    Maps and cartograms of 2004 US presidential election results

    GISuser WebMaps

    Dennis taken on July 11, 2005, at 9:15 a.m. EDT

    GISuser HOT Spots!

    Google Mashup Zone
    GISuser WebMaps
    Free Data Articles
    Spotlights & Tips
    GISuser Resumes
    Data Links
    10 Cool Things
    The LBS Zone!


    Partner Sites

    Geo Widgets

     


    Affiliations


    Get GISuser news & updates via RSS


    Mobilize / Share

    GIS / LBS Mosh
    Add to my Widsets


    Top Stops

    GISuser Site Login
    Username

    Password
    Forgotten your password?
    No account yet? Create one




    Spatial Media, LLC ©2003 - 2008 All rights reserved / Privacy Statement