How Google Map Works (part2): The geocoder






4.84/5 (9 votes)
Jul 24, 2007
1 min read

60231

659
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