Click here to Skip to main content
15,880,956 members
Articles / Operating Systems / Windows
Article

How Google Map Works (part2): The geocoder

Rate me:
Please Sign up or sign in to vote.
4.84/5 (10 votes)
27 Jul 20071 min read 59.6K   659   53   6
Ask GoogleMap for coordinates

Introduction

After having found how to get the image corresponding to geographic coordinates (view article), I wanted to look at another interesting point about Google maps: the geocoder. A geocoder is a service that allows you to find the longitude and latitude corresponding to a place name. I know the name of my street and city, but to get the image I need the geographic coordinates. This article explains how to ask Mr Google, who knows all this.

The request

To ask Mr Google for coordinates, you need to ask in the right way and tell him how to answer. To do this, you will have to create a web request with the URL:

http://maps.google.com/maps?output=kml&q=my street name

http://maps.google.com/maps will throw your request to the geocoder service. The output=kml parameter will tell Mr Google to answer in an easily comprehended way and the q=my_street_name parameter will indicate to Mr Google the point for which you need some information.

The answer(s)

I have found two possible answers: If there is no ambiguity about the place name, Google will send you the coordinates. If Google needs more precision, it will send back a list of possible place names. Then you will have to make another request with the right choice in the list. In the case of final response, the answer is an XML file that looks like this :

ASP.NET
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
    <name>New York, NY</name>
    <address>New York, NY</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
    <Point>
        <coordinates>-74.007130,40.714490,0</coordinates>
    </Point>
    <LookAt>
        <longitude>-74.007130</longitude>
        <latitude>40.714490</latitude>
        <range>64586.917969</range>
    </LookAt>
</Placemark>
</kml> 

However, if Google needs more precision, the answer will look like this:

ASP.NET
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Folder>
<name>Did you mean:</name>
<open></open>
<Placemark>
    <name>Paris, Lamar, Texas, United States</name>
    <address>Paris, Lamar, Texas, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Henry, Tennessee, United States</name>
    <address>Paris, Henry, Tennessee, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Edgar, Illinois, United States</name>
    <address>Paris, Edgar, Illinois, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Bourbon, Kentucky, United States</name>
    <address>Paris, Bourbon, Kentucky, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Logan, Arkansas, United States</name>
    <address>Paris, Logan, Arkansas, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Monroe, Missouri, United States</name>
    <address>Paris, Monroe, Missouri, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Mecosta, Michigan, United States</name>
    <address>Paris, Mecosta, Michigan, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Bear Lake, Idaho, United States</name>
    <address>Paris, Bear Lake, Idaho, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Stark, Ohio, United States</name>
    <address>Paris, Stark, Ohio, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
<Placemark>
    <name>Paris, Lafayette, Mississippi, United States</name>
    <address>Paris, Lafayette, Mississippi, United States</address>
    <styleUrl>root://styleMaps#default+nicon=0x304+hicon=0x314</styleUrl>
</Placemark>
</Folder>
</kml> 

In this case, take the address parameter and send a new request with this address. The provided Sample is a .NET control written in C#.

History

  • 24th July, 2007 -- Original version posted
  • 27th July, 2007 -- Article edited and moved to the main CodeProject.com article base

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


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGoogle GeoCode Pin
My name dammit !31-Jul-07 20:20
My name dammit !31-Jul-07 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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