By Jeevan Murkoth | Article Rating: |
|
October 15, 2008 10:30 AM EDT | Reads: |
357,482 |
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come.
What makes the service even better is the availability of the Google Maps API (Application Programming Interface) as a free Beta service. The API allows developers to embed Google Maps in their custom applications. It also allows them to overlay information on the map and customize the map to their needs. As I write this article there are quite a few sites that utilize Google Maps, and more and more of them are appearing by the day.
The API by itself is pretty straightforward and easy to use; however, it requires the developer to have a good command of JavaScript because it extensively relies on client-side Java scripting. In this article we will be looking at building a custom ASP.NET server control that would allow a .NET developer to harness the power of Google Maps in the code-behind model. We will see how to accomplish most of the functionality exposed by Google Maps using this control, and we'll also see how to data bind the control, thereby allowing developers to easily build data-driven custom ASP.NET Web applications. The control would eliminate the need for the developer to write any JavaScript to accomplish most of the Google Map functionality.
Some Google Maps Basics
Before we get into the details of the ASP.NET control, let's look at the basics of the Google Maps API. A detailed description of the API can be found at www.google.com/apis/maps/documentation/. 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. Each Web site that uses Google Maps has to have its own key. Make sure that you go through Google's Terms of Use (www.google.com/apis/maps/terms.html) before you start using Google Maps in your application.
Google represents an instance of the map as a "GMap" object. It is rendered as a div tag on the page. Once you have the map, it is possible to add controls to the map. Some of the available controls are the GMapType control that helps to toggle between the different views, namely map view, satellite view, and finally, the hybrid view that is a combination of map and satellite views. The other controls that are usually seen on the map are the ones used to add scrolling and zooming capability to the map. At the time of writing of this article, there are three different controls available:
- GLargeMapControl: A large control for scrolling and zooming
- GSmallMapControl: Similar to the previous one, but eliminates the zoom scale bar
- GSmallZoomControl: Includes only Zooming controls
Once the map has been set up, it is possible to overlay information on the map. The information can be in the form of points or lines, though points are the most common ones. In order to overlay a point on the Google Map, it's necessary to know its longitude and latitude. At this time, Google does not provide any geo-coding services that give the co-ordinates corresponding to an address, but there are a couple of free services available on the internet that do so. www.Geocoder.us is one of them and given a US address, it returns the longitude and latitude for the same. Once the longitude and latitude have been obtained, create an instance of a GPoint (which is Google's representation of a point on the map), then create a GMarker using this point and add the marker to the instance of the Google Map. In order to Center and Zoom on a point, the GMap Object exposes a method ZoomandCenter that takes the point and the level of zoom required as the parameter. Just like points, it is possible to overlay lines on the Map. Those of you who have used Google Maps for directions will be familiar with the lines used to depict the route. In order to add a line to the Google Map, we need to create an instance of a GPolyLine object and pass in an array of GPoints to plot it. It is also possible to assign color, width, and opacity to the line. Another useful feature in Google Maps is the ability to show a pop-up window when the user clicks on a Marker. Google Calls this pop-up window by the name "InfoWindow."
The Google Maps ASP.NET Control (GMAP Control)
The main aim of this control is to allow .NET developers to utilize the functionality of Google maps as a server-side control by writing little to almost no JavaScript at all. It is more of a .NET wrapper around the Google API; however, because it is a full-fledged ASP.NET server-side control, it is possible to bind data to the control, thereby increasing the usability of Google Maps. In the following sections we will see how to use this control to implement most of the common functionality of Google Maps. Before we do so, let's take a look at the control. The principal class of the control is the "GMapControl" class. This class in turn references the following classes (most of these classes are the .NET equivalents of the classes used by Google):
- GPoint: This is the class representation of a geographical point and exposes latitude and longitude as its two properties.
- GPoints: This class represents a collection of GPoint objects.
- GIcon: Represents a custom icon that is used as an overlay on the map. The GIcon class exposes the following properties: the Image URL, which as the name suggests, is the URL of the image used to represent the icon; ShadowImageURL is the URL of the shadow associated with the icon; IconSize and ShadowSize represent the size of the icon and the shadow, and the last two properties are IconAnchor and InfoWindowAnchor, which specify the point where the icon should be anchored to the map relative to its top-left corner and the point where the Info window should be anchored to the map.
- GLine: This is a line that the user wishes to overlay on the map. By default it takes a collection of points (GPoints) as an argument in its constructor. It is also possible to set the color of the line as well as its weight and opacity through an overloaded constructor.
- GMarker: This is the .NET representation of the Google Maps class GMarker. The default constructor accepts an instance of a GPoint class. It also has an overloaded constructor that takes a GIcon along with the GPoint in case the developer wishes to use a custom icon to represent the marker.
- GSize: Represents a two-dimensional size measurement.
- JScriptGenerator: This is an internal class and has more of a helper function. It generates most of the JavaScript functions that are needed by the control.
- HelperColorConvertor: This class is used to convert a color into its equivalent Hex value. This class is marked as internal.
- HelperDataResolver: This is an internal class that helps in data binding and has just one method. The method casts a datasource object into an object that implements the IEnumerable interface. The help file that describes in detail the different methods and properties of the classes involved is available as a download.
Getting Started in ASP.NET
Before we use the ASP.NET control in our application, there are a few things that need to be taken care of to ensure that it works as desired.
Web.config File
The GMAP control renders itself as a DIV tag, however for non-Internet Explorer Browsers, ASP.NET renders the div tag as tables. If you want the page to render the GMAP control properly in other browsers such as Netscape and Firefox, include the browser cap section shown in Listing 1 into the Web.config file of your application.
Page Configuration
Google has certain recommendations for the HTML standards on pages that contain the map to make sure that the layout is predictable across different browsers. A detailed description of the same can be found in the Google maps documentation. It is imperative that you follow these standards, especially if you plan to overlay lines on your map. For lines to be rendered on the map, you need to include the VML namespace in the HTML page for Internet Explorer browsers. Make sure that you don't forget to do this, because otherwise the lines will not be displayed in Internet Explorer. The HTML tag of your page should at the minimum look like the snippet below:
<HTML xmlns:v="urn:schemas-microsoft-com:vml">
Adding to the Toolbar
This step is optional; however if you are using Visual Studio .NET as your IDE, I would recommend that you go ahead and add the GMap control to your toolbox. The advantage of doing so is that you can easily drag and drop the GMap control onto an ASPX page like any other ASP.NET control, and Visual Studio will automatically register the control on your page. Figure 1 shows the GMap control on the toolbar.
Creating a Basic Map Using the Control
Since we are done with the setup, let's go ahead and create a simple example using the control. We will add the GMap control to the page, set its dimensions, and make it center and zoom at a particular point. For the sample application used in this article, I have saved the map key in the Web.config file and will be setting the GoogleMapKey property of the control from the config file. I will be setting the map type of the control to be that that of "Map." In case no map type is specified, the control defaults to the preset "Map." The GMap Control also supports satellite and hybrid map types. Make sure that you center and zoom at a point, otherwise all that will show up will be a grey area. Listing 2 shows the code for this example and Figure 2 shows the output.
Setting the GMap Control Properties
Let's go ahead and set some properties to the basic example we just created. The GMap control exposes a set of properties that allows the developer to customize the control to his or her needs. If we wish to give the user the flexibility to change the view, we set the ShowMapTypeControl property of the control to true. By default, the user is able to drag the map, however, if we do not wish the user to drag the map around, we can set the EnableDragging property to false. To allow the user to be able to scroll or zoom, set the ScrollControlType property of the control. There are three different options: "large," "small," and "zoom only," to correspond to the controls offered by Google. Listing 3 shows the source code and Figure 3 shows the output in the browser.
Published October 15, 2008 Reads 357,482
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Jeevan Murkoth
Jeevan Murkoth is a Microsoft Certified Solutions Developer (MCSD) in .NET (Early Achiever) and a Microsoft Certified Application Developer (MCAD) in .NET. He currently consults for Tennessee Valley Authority and lives in Chattanooga, TN. He has an MS in Management Information Systems from Texas Tech University.
![]() |
stuartlodge 01/08/09 08:00:49 PM EST | |||
Is anyone else developing this control forwards? Can I update the control and republish it? (with credits obviously). e.g. I've just spent a couple of hours updating bits to GMap2 so as to use GLatLngBounds - http://slodge.blogspot.com/2009/01/adding-bounding-box-to-google-maps-in... |
![]() |
punkbd 12/15/08 10:45:53 AM EST | |||
Where is the control? Very unfriendly site. On 'Your Feedback' what's the point of have 'read more & respond' link if it does nothing. Really stupid. |
![]() |
stuartlodge 12/09/08 04:41:47 AM EST | |||
Zachary - if you click on any of the code sample page links then there are additional download options at the top of the code sample pages - the sample code download includes the control. One word of warning - the zip file contains lots of Apple Mac (Vista unfriendly) thumbnail files. |
![]() |
stuartlodge 12/09/08 04:39:57 AM EST | |||
Thanks for this excellent article and control. I've put the code for a fixed JScriptGenerator.cs on my blog at http://slodge.blogspot.com/2008/12/jscriptgeneratorcs-with-fixes-for.html - this fixes the addOverlays problems and the GMAP_TYPE problems. |
![]() |
zwheeler 12/08/08 04:55:25 PM EST | |||
where is the gmap control.so i can download it |
![]() |
erom 11/19/08 05:40:46 AM EST | |||
I downloaded and tried to run the SampleSolution through Visual Studio. Every time that I am running the pages (.aspx) which exist in the SampleSolution I am getting the same errror 'G_MAP_TYPE' is undefined. Can someone suggest a possible solution? |
![]() |
sahil_mohali 10/15/08 07:13:12 AM EDT | |||
asdsdfv fdfd |
![]() |
sahil_mohali 10/14/08 11:19:55 AM EDT | |||
How to use onmouseover on marker with c# code, by default when u click on marker then info window open, i want this with onmouseover????????? |
![]() |
sahil_mohali 10/14/08 07:57:47 AM EDT | |||
help |
![]() |
sahil_mohali 10/14/08 05:26:08 AM EDT | |||
any one please help regarding this? |
![]() |
sahil_mohali 10/13/08 02:57:44 PM EDT | |||
How to use onmouseover on marker with c# code, by default when u click on marker then info window open, i want this with onmouseover????????? |
![]() |
webpossible 09/27/08 04:34:04 PM EDT | |||
ah just found the source, you need to click on list 1 from the main article and then click on the first link in this page. easy when you know where to look. |
![]() |
webpossible 09/27/08 03:03:20 PM EDT | |||
where can I download this marvelous Google Maps ASP.NET Control (GMAP Control) from ? I cant seem to find a link for this anywhere. Thanks for your help and for writing this excellent article. |
![]() |
Noble 07/25/08 01:45:27 AM EDT | |||
I got the same java script error in JScriptGenerator.cs with Can You Please put your full JScriptGenerator.cs |
![]() |
Sajid Mushtaq 04/09/08 07:42:55 AM EDT | |||
i cant find a link to download the source code and the control. Please help. Regards, |
![]() |
Shabdar 03/25/08 11:53:05 AM EDT | |||
I have created similar control. It works with latest versions of Google Maps API. Here is the link, http://www.shabdar.org/google-maps-user-control-for-ASP-Net-part1.html |
![]() |
Ghata 03/24/08 10:51:31 AM EDT | |||
This one works with GMAP2 http://www.shabdar.org/google-maps-user-control-for-ASP-Net-part1.html |
![]() |
Balvinder 01/10/08 12:44:05 AM EST | |||
I have downloaded GMap control and sample applications. Following code does not works. Please, tellme what is going wrong ? Suggest any modifications in code. GMapControl1.Width=400; |
![]() |
Sai 12/14/07 05:24:08 AM EST | |||
There is a change in google api and this shows a javascript error. So please remove "this.reOrderOverlays();" from GenerateNewOverLayFunction() in JScriptGenerator.cs file. You may find more info on this at http://groups.google.com/group/Google-Maps-API/browse_thread/thread/96fe... |
![]() |
baybay 12/03/07 10:25:36 PM EST | |||
This article is horrible. Very generic. Don't waste your time. |
![]() |
fabrice 11/19/07 05:27:23 AM EST | |||
How do you deal with events ? |
![]() |
Derek 11/18/07 10:18:07 PM EST | |||
Here's the fix for the marker issue that everyone seems to still be having a problem with. In JScriptGenerator.cs replace with the overlays.push is what looks to be causing the problem. After making this fix, I can lay markers on the map. Cheers |
![]() |
Josh 10/01/07 11:54:26 AM EDT | |||
Patrick - Here is good easy to use control with full functionality. I did have a problem in deployment with the key, so I would recommend testing it on your server before you do a lot of development. |
![]() |
Patrick 09/30/07 02:49:43 PM EDT | |||
Hi Jeevan, |
![]() |
Jeevan 08/27/07 05:04:32 PM EDT | |||
Hi Josh, Thanks |
![]() |
Josh 08/27/07 04:34:59 PM EDT | |||
Great, but I kind of need to display markers. Its actually the key part of displaying the map. After playing around with this a while, markers or custom markers will not display. Several others had the same problem. Any ideas, or just skirting the issue? |
![]() |
Eric 08/13/07 01:31:52 PM EDT | |||
Where do I go to download the GMap Control. I was lost trying to follow the step. |
![]() |
san 08/02/07 08:20:00 AM EDT | |||
I have downloaded GMap control and sample applications. Following code does not works. Please, tellme what is going wrong ? Suggest any modifications in code. GMapControl1.Width=400; |
![]() |
Priya 07/19/07 01:02:11 PM EDT | |||
I am getting an error in reOrderOverlays(). It says object dosen't support this property. I am getting a java script error. Is there a fix for this? Please let me know. |
![]() |
Rhys 07/10/07 04:31:10 AM EDT | |||
This looks awfully plaguerised from an article at another site... Naughty, naughty, very naughty! |
![]() |
Troy Johnson 06/08/07 10:52:38 AM EDT | |||
Anyone know if this is or will soon be updated to use GMap2? |
![]() |
Niketa 06/05/07 11:32:18 PM EDT | |||
this code is working fine but i am not able to draw lines and could not use marker. please help me in this. i had to commen reOrderOverlays() funstion call as my code was not working . please suggest me how can i resolve this. Thanks a lot. |
![]() |
Ridhi 06/05/07 11:24:10 PM EDT | |||
Thanks Jeevan , but i am not able to see marker and polylines or lines using this tool , please help me in this . i have tried simple code . i had to comment reOrderOverlays() to make this tool working. Please suggest the way to resolve it or problem. |
![]() |
Tom 04/23/07 06:57:02 PM EDT | |||
Thanks for this tool Jeevan, unfortunately I can't seem to get any markers to display (have tried the sample code as well as different browsers). No errors, just no markers! I noticed some others had this issue. Do you have any idea what may be causing this as I'd love to be able to use it properly! |
![]() |
Gordon Hooton 03/29/07 11:01:25 PM EDT | |||
Excellent article, is thee any way to extend the properties of the MapControl to return the current latitude, longtitude of the displayed map centre and the zoom level ? I know these are available in Google Maps Javascript. |
![]() |
Mohsen Pourett 02/16/07 01:14:13 PM EST | |||
I have downloaded the code and sample project. I have converted the code to vb.net and have been able to display the basic map properly (MapControl.dll is dated (10/9/2005). However, I have not yet to succeed in getting markers and info popups to show. The version downladed also does not have any databinding methods or attributes! any suggestions /help will be appreciated. Unfortunately, I can not provide a URL to the project since it is on Intranet servers. The code for the page with difficulty is as follows: 1. codeBehind: Imports System Public Class BasicMap #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. End Sub 'NOTE: The following placeholder declaration is required by the Web Form Designer. Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Class 2- html: <%@ Register TagPrefix="cc1" Namespace="MapControl" Assembly="MapControl" %> BasicMap v\:* {BEHAVIOR: url(#default#VML)} 3- Page error: Line: 32 |
![]() |
Jimi Schacht 02/08/07 04:10:01 PM EST | |||
Awsome! At last a .Net wrapper for the API that actually works!... ...But, for some reason i cannot get markers or lines to display. I use your example code and can't get the to show up with or without messing with the code. Is issue? |
![]() |
Jimi Schacht 02/08/07 04:08:54 PM EST | |||
Awsome! At last a .Net wrapper for the API that actually works!... ...But, for some reason i cannot get markers or lines to display. I use your example code and can't get the to show up with or without messing with the code. Is issue? |
![]() |
Joginder 11/05/06 01:46:25 PM EST | |||
Hi, I am very much interested in the source code that shows how to handle click events on the map using Jeevan's control. If the project is in ASP.NET developed using VB.NET would be even better. Regards, |
![]() |
Jay Scott 09/19/06 08:14:30 AM EDT | |||
Thank you Jeevan. Is there a way of outlining an entire area? For instance a map of the US would have each of the states outlined, clicking on one would zoom in and show different points within the state. |
![]() |
Jeevan 09/19/06 07:44:26 AM EDT | |||
Hi Jay , |
![]() |
Jay Scott 09/18/06 10:52:35 AM EDT | |||
Your article is great. What I could not find was a way to download the control. |
![]() |
SYS-CON Belgium News Desk 09/14/06 03:36:38 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
SYS-CON Brazil News Desk 09/14/06 03:12:23 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
SYS-CON Australia News Desk 09/14/06 01:53:52 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
Enterprise Open Source News Desk 09/14/06 01:09:42 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
SYS-CON Brazil News Desk 09/13/06 05:30:17 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
n d 09/13/06 03:14:52 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
n d 09/12/06 07:00:05 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
![]() |
n d 09/12/06 06:49:42 PM EDT | |||
I am sure that most of you have heard about or have had a chance to use Google Maps. It's a great service and I was really impressed by the responsiveness of the application and the ease with which users could drag and zoom maps from a Web browser. It has in many ways heralded the arrival of AJAX (Asynchronous JavaScript and XML), which I am sure will revitalize Web development in the days to come. |
- Cloud People: A Who's Who of Cloud Computing
- Twelve New Programming Languages: Is Cloud Responsible?
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Infrastructure Was Then, Private Cloud Is Now and Beyond
- Advanced Laser Technologies Laser Cuts Powder Coated Parts to Assure Clean Opening Edges
- You're Gonna Get Some Walk-Ons!
- Acer America introduces Aspire S7 Series, the World's Thinnest Touch Ultrabooks
- Rock – till You Drop – to The Temptations at Cloud Expo Silicon Valley!
- 11th Cloud Expo: A–Z of Big Data & Cloud Computing Topics
- Understanding Remote Desktop Services (RDS)
- Cloud Expo Silicon Valley: Lunchtime Focus Keynote - Day 2
- Cloud Expo Silicon Valley: This Is Your Career on OpenStack
- Cloud People: A Who's Who of Cloud Computing
- Twelve New Programming Languages: Is Cloud Responsible?
- Eleven Reasons Why Windows Phone Will Overtake Android
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Thanks to Big Data, Analytics Will Be a $51B Business by 2016: IDC
- Cloud-Based Super Computing at Cloud Expo Silicon Valley
- Enterprise Java EE PaaS - OpenShift vs Google App Engine for Java
- Infrastructure Was Then, Private Cloud Is Now and Beyond
- Apple Spends Only 1/7 on R&D Compared to Google, Microsoft
- Cloud Expo Silicon Valley: Lessons from Managing 500+ AWS MySQL Instances
- Advanced Laser Technologies Laser Cuts Powder Coated Parts to Assure Clean Opening Edges
- You're Gonna Get Some Walk-Ons!
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Where Are RIA Technologies Headed in 2008?
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- ');
for(i = 0; i < google_ads.length; ++i)
{
document.write('
- ');
document.write('' + google_ads[i].line1 + '
'); document.write('' + google_ads[i].visible_url + '
'); document.write(google_ads[i].line2 + ' ' + google_ads[i].line3); document.write(' ');
}
document.write('