If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Google Maps with ASP.NET

by Farooq Kaiser 28. January 2009 10:48

Today, i'll show you how to integrate google maps with asp.net page.

The first step before using Google Maps is to register for a key with Google 

(www.google.com/apis/maps/signup.html). 

This is absolutely free and hardly takes a few minutes. 

Add a DIV tag to the .aspx page.

<div id="map" style="width: 450px; height: 400px; border: 1px solid #979797;"></div>

In the code-behind file, add the following code to the Page_Load event handler

  protected void Page_Load(object sender, EventArgs e) {

  if (!(this.Page.ClientScript.IsClientScriptBlockRegistered("onload")))        {

   //Replace your address here that you want in google map   
  string Address= "Torono, on"; 

   //Replace you google API key here
  string GAPIKey = "xxxxxxxxxxxxxxxxxx"; 
 
  StringBuilder sb = new StringBuilder();
  sb.AppendFormat("<script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key={0}\" type=\"text/javascript\"></script>", GAPIKey);
  sb.Append("<script type=\"text/javascript\" language=\"javascript\">var map = null;var geocoder = null;");
  sb.Append(" window.onload = function(){if (GBrowserIsCompatible()){map = new GMap2(document.getElementById(\"");
  sb.AppendFormat("{0}\"));", "map");
  sb.AppendLine("map.addControl(new GLargeMapControl());map.addControl(new GMapTypeControl());geocoder = new GClientGeocoder();");
  sb.Append("if (geocoder){geocoder.getLatLng(");
  sb.AppendFormat("'{0}'", Address);
  sb.Append(",function(point) {if (point){map.setCenter(point, 13, G_NORMAL_MAP);"); 
  sb.Append(" var marker = new GMarker(point);map.addOverlay(marker);");
  sb.AppendFormat("marker.openInfoWindowHtml('{0}');", Address); 
  sb.Append("}});}}}</script>");
  this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "onload", sb.ToString());
  } }

 Please feel free to leave any comments.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

.NET | API | asp.net | C# | Google

Jobs Autos Real estate Videos Power by Google