Click here to Skip to main content
Click here to Skip to main content
Go to top

How Google Map Works (part2): The geocoder

, 27 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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 :

<?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:

<?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

Share

About the Author

Pascal Buirey
Web Developer
France France
No Biography provided

Comments and Discussions

 
GeneralThank You... PinmemberVasudeva Kiran26-Aug-08 20:29 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 27 Jul 2007
Article Copyright 2007 by Pascal Buirey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid