Using Google Maps, Geocoding and #.NET

Google Maps using C# .NET

The Google Maps API lets us embed Google Maps in our own web pages with JavaScript. The API provides a number of utilities for manipulating maps and adding content to the map through a variety of services, allowing us to create robust maps applications on our website.

Google Map Key

We must have a Google Account to get a Maps API key, and our API key will be connected to our Google Account.

  1. There is no limit on the number of page views you may generate per day using the Maps API.
  2. There is a limit on the number of geocode requests per day. The Limit is set on 50,000 queries per day, Google allows to increment the limit with direct contact with Google.
  3. The Maps API does not include advertising.
  4. The service must be freely accessible to end users.
  5. You may not alter or obscure the logos or attribution on the map.
  6. Google will upgrade the APIs periodically.

The site to get our own Key:

Developing the Library

Create a new project or add a new class where we are going to place all the Google Maps methods, function, etc:

1. Add Google Map Key:

It’s a good idea to develop to get our Key, so it will be easier to change it and use it on different google queries.

public static string GeocoderKey()
//Google Map Key by the moment is just for localhost, we have to change it.
return "ABQIAAAAKji2yEvnKQltot2jp7sguBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQMXl5qiRVRMsOEG5s9HrQqdXPl1w";

2. The Query Builder

The three parameters received forthis function could be used for different purposes, such as the three parameters could be: City, State, Country or Postal Code, State, Country. This is going to be explained later.

While building the query  we call the function that has our API Key, the API Key is needed on every request.

The query will be executed and the result is a XML file, that is going to be saved on a XML Document (Object) which we are going to use and manipulate.

public static XmlNodeList GoogleQuery(string text1, string text2, string text3)


XmlDocument xDoc = new XmlDocument();

xDoc.Load("" +  HttpUtility.UrlEncode(text1) + "," + HttpUtility.UrlEncode(text2) + "," +  HttpUtility.UrlEncode(text3) +  "&output=xml&sensor=false&key=" + GeocoderKey());

XmlNodeList personas = xDoc.GetElementsByTagName("Response");

XmlNodeList list = ((XmlElement)personas[0]).GetElementsByTagName("Placemark");

return list;


The result is a XmlNode with the information that google retrieves, it could be all the needed data, the geographic data or the error that google retrieves: like no more queries, or wrong data inserted on querystring.

An example of the XML file retrieved by the API:

   <name>12 Calle,Guatemala,Guatemala</name>
   <Placemark id="p1">
     <address>12 Calle, <a class="zem_slink" title="Guatemala City" rel="geolocation" href=",-90.5352777778&spn=0.1,0.1&q=14.6133333333,-90.5352777778%20%28Guatemala%20City%29&t=h">Guatemala City</a>, Guatemala</address>
     <AddressDetails Accuracy="6">
               <ThoroughfareName>12 Calle</ThoroughfareName>
       <LatLonBox north="14.6042849" south="14.5979896" east="-90.5088887" west="-90.5254774"/>

3. Data Manipulation:


Geolocation refers to the identification of the geographic location of a user or computing device via a variety of data collection mechanisms. Most geolocation services use network routing addresses or internal GPS devices to figure this location.


Is a position or point in physical space that something occupies on the Earth’s surface, the Solar System, or mankinds physically reachable universe. An Absolute location can often be designated using a specific pairing of latitude and longitude, a Cartesian coordinate grid.

3.1. Geographic Location from an address:

The library request the geolocation coordinates for a specific address, the three needed values are the city, state and country:

First the attribute accuracy is checked to determine if the retrieved file has the requested values or if it just has the error information. to check we use the following check:

node.GetAttribute("Accuracy") == "6"

The number 6 was determined by Google, it means that the query was executed successfully and it retrieves the needed data. Check status codes for more info.

We send the follow parameters to the query builder method:

  1. City: This parameter is optional, but if a more specific location is needed, it will be requested.
  2. State: This parameter is not optional.
  3. Country: This parameter isn’t optional, It is needed to be more specific and to use it as limit, google will search just in the selected country.
XmlNodeList coordinates = nodo.GetElementsByTagName("coordinates");

XmlNodeList LatLonBox = nodo.GetElementsByTagName("LatLonBox");

string test = coordinates[0].InnerText;

char[] delimiterChars = { ',' };

string[] words = test.Split(delimiterChars);


foreach (XmlElement nodo2 in LatLonBox) //the data for the bounding boxes


if (i < 2)


LatLon[0] = words[0];//latitude

LatLon[1] = words[1];//longitude

LatLon[2] = nodo2.GetAttribute("north");

LatLon[3] = nodo2.GetAttribute("south");

LatLon[4] = nodo2.GetAttribute("east");

LatLon[5] = nodo2.GetAttribute("west");




LatLon[0] = "The postcode does not exist";

LatLon[1] = "";

LatLon[2] = "";

LatLon[3] = "";

LatLon[4] = "";

LatLon[5] = "";



We use the  XMLNode list retrieved to select the latitude and longitude node that has the following info:

  1. Latitude
  2. Longitude
  3. North
  4. South
  5. East
  6. West

The data is saved on a string list that could be displayed as the code wanted, for example:

Latitude:   -72.1179169

Longitude: 42.4328338

North:        42.4845309

South:       42.3566919

East:        -72.0235210

West:       -72.2051740

3.2. Geographic Location from postal/zip code:

The library requests the Geolocation coordinates for a postal code, it needs to send the exact postal code and the country, the state is optional.:

As before, we first check the “Accuracy” attribute to verify if we got the level of information we need, for what we are looking for we need for it to return a “6” you can  Check the status codes for more info on google’s webiste.

node.GetAttribute("Accuracy") == "6"

We send the following parameters to the query builder method:

  1. Postal Code: This parameter is required, since it is basis of what we are looking for.
  2. State: This parameter is  optional, it could be sent as null or empty value.
  3. Country: This parameter is required. It limits google’s query to a single country.

Once again we use the XMLNode object returned to get, on the latlong node, the coordinates we need to locate the postal code.

  1. North
  2. South
  3. East
  4. West
  5. <coordinates>-90.5401602,14.6037439,0</coordinates>

and the data is saved on a string list that could be displayed as the code wanted, for example:

3.3. Status Codes

The “status” field within the Geocoding response object contains the status of the request, and may contain debugging information to help you track down why Geocoding is not working. The “status” field may contain the following values:

  1. “OK” indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
  2. “ZERO_RESULTS” indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or alatlng in a remote location.
  3. “OVER_QUERY_LIMIT” indicates that you are over your quota.
  4. “REQUEST_DENIED” indicates that your request was denied, generally because of lack of a sensor parameter.
  5. “INVALID_REQUEST” generally indicates that the query (address or latlng) is missing.

4. Using the data

The retrieved points can be used for different purposes, for example:

  1. Bounding Box: For this is needed four points: south, north, east and west. This could be used to know all the geographic points inside the box. To know the limits for a city, state, country.
  2. Geolocation: To know the exact geolocation (latitude and longitude) for the postal code or the address.
  3. Display point on map: The latitude and longitude could be used to display the address or postal code on one google map:

This is just a brief overlook of what you are able to accomplish by using one of Google’s easiest to use APIs, with just a few details about the place you want to find you are able to get enough information to feed into a map to display on your page. You get enough information to build a “bounding box” around your point of interest that can help you or your users narrow down searches.

This entry was posted in .NET, ASP, C#, software development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s