Jatin kacha's Blog

How to call Google MAP Webservice API from asp.net, call Geocoding Map API Webservice from asp.net

Posted on: October 2, 2010

How the necessities arise?

Necessity is mother of invention.

I was assigned a task to develop a restaurant portal.

The basic aim behind creation of this project is to have a marketplace for all restaurant owner or hotel business, and provide end user with integrative google map services so that they visualize realtime.

If we are supposed to insert a simple google map, then we just have to insert a piece javascript code, and boom you are done.

But in my project, I have requirement where different restaurant/hotel owner register and give us their details and addresses.

So, What is Google Map API?

The Google Maps API lets you embed Google Maps in your 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 you to create robust maps applications on your website.

All map related functionalities can be achieved as seen on http://maps.google.com/

Then what was hurdle for me in using Map API?

These are the pure JavaScript code..!! You can develop whatever rich Map application you wanted to develop by writing piece of JavaScript codes.

Actually to host a map on your page, you need to have two parameters, Latitude & Longitude. But how do I get lat. and long. from server side scripting?

But I had no idea how to call Google Map API from C#/VB.NET.

So for my project, as usual, I start googling to find out my piece of sweet.

After searching for couple of hours, I come across this good article.


Here the author explained the how to obtain the latitude and longitude co-ordinates for an address using the Google Maps API.

But again here, it is pure JavaScript that utilize Google Map API to get lat & long of any address. But my problem is still there?

Hurrray….. I found the solution

Then accidently I come to across Google Maps API Web Services.

It is basically Maps API Web Services, a collection of HTTP interfaces to Google services providing geographic data for your maps applications.

In the documentation (http://code.google.com/apis/maps/documentation/webservices/),

I see that you can utilize Google Map API web service by making HTTP request to a specific URL, providing necessary parameters with URL. Which in return, gives a response in desired format.

You can get response inform of JSON or XML.

Say for e.g. it can be called like this URL

http://maps.google.com/maps/geo?q=khandala, maharashtra&output=xml&key=xxxxxxxxxxxxxx

Paste it into your browser and you will receive a XML response.

Immediately, I get one “Mantos (Dimag ki batti jala de)” and found a clue J

I thought why not to develop a library that make a call to this URL and parse the returned output to get lat & long…!!?

So for that purpose I write this library.

Obviously, before doing all these, you will need to register with Google Map API and get a key so that you can utilize their API.

Sign up and get a Google Maps API Key.  You will need one for the domain name of where you will be hosting the map.  You can get your API Key from here (http://code.google.com/apis/maps/signup.html)

Following is the complete code that I used to call Google Map Geocoding API from Asp.net.

All you have to do Is to just create a class file and paste the following code. And you are ready to call Google Map Geocoding API.

How to call Geocoding API from Asp.net?

Step 1

/// Resolve addresses into latitude/longitude coordinates using Google MAP API webservices

public static class Geocoder{

private static string _GoogleMapsKey = Config.getAppSetting(“GoogleMapsKey”);

/// Google.com Geocoder

/// Url request to

/// http://maps.google.com/maps/geo?q=your address&output=xml&key=xxxxxxxxxxxxxx

public static Geolocation? ResolveAddress(string query)


if (string.IsNullOrEmpty(_GoogleMapsKey))

_GoogleMapsKey = ConfigurationManager.AppSettings[“GoogleMapsKey”];

string url = http://maps.google.com/maps/geo?q={0}&output=xml&key=” + _GoogleMapsKey;

url = String.Format(url, query);

XmlNode coords = null;


string xmlString = GetUrl(url);

XmlDocument xd = new XmlDocument();


XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable);

coords = xd.GetElementsByTagName(“coordinates”)[0];


catch { }

Geolocation? gl = null;

if (coords != null){

string[] coordinateArray = coords.InnerText.Split(‘,’);

if (coordinateArray.Length >= 2)


gl = new Geolocation(Convert.ToDecimal(coordinateArray[1].ToString()), Convert.ToDecimal(coordinateArray[0].ToString()));



return gl;


public static Geolocation? ResolveAddress(string address, string city, string state, string postcode, string country)


return ResolveAddress(address + “,” + city + “,” + state + “,” + postcode + ” “ + country);



/// Retrieve a Url via WebClient

private static string GetUrl(string url)


string result = string.Empty;

System.Net.WebClient Client = new WebClient();

using (Stream strm = Client.OpenRead(url))


StreamReader sr = new StreamReader(strm);

result = sr.ReadToEnd();


return result;



public struct Geolocation


public decimal Lat;

public decimal Lon;

public Geolocation(decimal lat, decimal lon)


Lat = lat;

Lon = lon;


public override string ToString()


return “Latitude: “ + Lat.ToString() + ” Longitude: “ + Lon.ToString();


public string ToQueryString()


return “+to:” + Lat + “%2B” + Lon;



Step 2

Now, you just need to put a key in AppSetting section that hold your Google Map API key.

Step 3

I have provided 2 overloaded methods to call Geocoding Map API Webservice, named ResolveAddress.

Say for e.g. you can call method like this.

Geocoder.ResolveAddress(“University road”,”rajkot”,”gujarat”,””,”India”)

I hope this will solve your purpose, those who wanted to develop Geocoding application, those who want to access Google Map Webservice API from c#/vb.net.

Happy programming…


30 Responses to "How to call Google MAP Webservice API from asp.net, call Geocoding Map API Webservice from asp.net"

Thanks for providing the code. I would like to implement it. The Google Maps API is the most used code on the web. It lets you embed Google Maps in your own web pages with JavaScript.


Really, good article… 🙂 thanks. My friend !

[…] The busiest day of the year was November 9th with 34 views. The most popular post that day was How to call Google MAP Webservice API from asp.net, call Geocoding Map API Webservice from asp.net . […]

Proficient article to know about integration of google map.

Thanks siddharth,

Your valuable comments makes us to get motivated. 🙂

thanks for this usfull code; but i created a new class then i past the code in Step1 inside it , but there were a lot of errors such as XmlDocument() is not found , etc.
so i can solve these issues?.


first you need to include required namespace to work with .net XML API. If you are using VS, VS itself is capable of instructing you which is missing namespace to include.

can you post what i should write for the HTML code to display this map?
and i have created a class in my MVC asp.net application , but the folloing statment raised an error:-
private static string _GoogleMapsKey = Config.getAppSetting(“GoogleMapsKey”);
so i changed it to:-
_GoogleMapsKey = ConfigurationManager.AppSettings[“GoogleMapsKey”];
is this ok?

[…] came across this excellent post by Jatin Kacha on how to call the Google Map WebService API from asp.net and though to change it slightly to […]

nice dude

thanks for your feedback. 🙂

can u tell the whole coding on aspx and aspx.cs page


I am not very sure what you are asking for?

I have mentioned step 1 through step 3 that is the raw cade.
You can put them directly into your project.

I was wondering can you use the google maps api to return if a addrss location was inside a shape I have drawn on a map linked to my google account / api key.

All I want returned is xml data telling what map the location is found in.
For example I have territories mapped out on a google map and I want to know if a customers location is in a sales reps territory. I don’t need to see the map just the xml data.

i would like to do the same thing!!!!


Hope this article would have helped you.

Thanks for the link to my webcodeblog article using javascript for geocoding using Google Maps API. Just a comment to let you know that this link has now moved and a newer version of this article exists at: http://stefanzvonar.com/2012/06/25/obtain-latitude-and-longitude-co-ordinates-for-an-address-using-asp-net-and-the-google-maps-api-v3/


i need of how to create link into Google map my company address the code in c# with code sent me this mail id ple….

Dear Rajesh,

Really sorry, but i was unable to parse your english properly.
Do you want a a code that display a google map with your company’s address location?
If so, then you don’t need c# code, you can simply do that using a simple JS script.

Hi Jatin,
Excellent post.
how to test the samething in your development environment cause API key is bound to your domain only ?


Sure, you can do the same in you local VS development.
All you have to do is configure your website in Visual Studio to run in static port, rather than dynamic port.
So for e.g. if you configure a port as 8734, then your localhost URL will be like: http://localhost:8734/default.aspx. This same URL you can submit while generating Google map key.

Hope that helps you.

Thanks Jatin for your answer. so google API key should be registered as localhost: or my :localhost ?

Hi Jatin

above code is very usefull, still am fighting for it to work , can u plz tell me what exactly namespace is used for geolocation ???

Thanks & Regards

There is no .net specific lib. you need to use.
Just copy paste the same code in your VS class file, and rest studio will suggest you which namespace are missing.

thanx alot for the reply more over i am able to get latitude and longitude now i have tried both the ways from java script and Code behind method once again thanks alot for the nice post 🙂

can this code can be used to get the driving distance b/n two lat/lng values

It can be, depends on your use case. This code is basically to get lat/long.

i wanted to get the driving distance b/n two lat/lng values on asp.net using c# class and i couldn’t find any on the web this posting is a the closest i found. can u modify this so it can give me the driving distance b/n two points


I will write on this soon. Btw, have you gone through Google doc. on how to get distance between locations? You may get hint from it.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

My tweets

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: