User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

    A Google Maps .NET Control



    Introduction

    This article examines a custom ASP.NET server control I developed to make using the Google Maps API easier for .NET developers. This article assumes you are somewhat familiar with Google Maps (or are at least somewhat curious). If you're not familiar with Google Maps or its sweet API, check out http://maps.google.com/ and http://www.google.com/apis/maps/documentation/ for the lowdown. You may see references throughout the article to "my control, my GoogleMaps control, my GMap control, and so forth." I did not create the Google Maps API; I merely used C#, XML, and XSL to create a wrapper for ASP.NET.

    As I stated above, my main goal for this control was to make it easy to use a Google Map using .NET languages. I also tried very hard not to use any fancy or proprietary "hacks" or workarounds when creating this control.

    Setup & Configuration

    I need to point out a few quick things before you get to the examples. The Google Maps Documentaion makes some specific recommendations for the DOCTYPE, HTML Namespaces, and STYLES for your Google Map pages. Each of the example pages starts like so:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
       <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:v="urn:schemas-microsoft-com:vml">
    
    <head>
       <meta http-equiv="content-type"
             content="text/html;
             charset=UTF-8"/>
    
       <title>. . .</title>
       <style type="text/css">
          v\:* 
          {
             behavior:url(#default#VML);
          }
       </style>
       . . .
    
    </head>
    

    Note the "strict" DOCTYPE, the custom xmlns attributes in the HTML tag, and the cryptic-looking stylesheet entry for v\:*. If you don't include these values in your pages, strange things may happen. I noticed them especially when using polylines in Internet Explorer.

    Google API Key

    You'll need a valid Google API key (get one here) to run the examples. The key included in the downloadable code (see the web.config file) will work as long as your application is located at http://localhost/TestWeb/.

    Web.Config Setup

    The GMap control is based on the "div" tag. Unfortunately, ASP.NET renders div tag controls as tables in non-IE browsers by default. For the control to render properly in Firefox, you need to add the following entries to your web.config or machine.config file:

    <browserCaps>
       <case match="^Mozilla/5\.0\s*([^)]*\))\s*(Gecko\/\d+)\s*Firefox\/
       (?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)).*">
    
    
       type=Firefox/${version}
       version=${version}
       majorversion=${major}
       minorversion=${minor}
       frames=true
       tables=true
       cookies=true
       javascript=true
       javaapplets=true
    
       ecmascriptversion=1.5
       w3cdomversion=1.0
       css1=true
       css2=true
       xml=true
       tagwriter=System.Web.UI.HtmlTextWriter
       <filter match="^b" with="${letters}">
               beta=true
    
       </filter>
       </case>
    </browserCaps>
    

    It's a good idea in general to add these values to the machine.config file on your Web server. Why? So things like asp:panel and other "div"-based controls will render properly in Firefox.

    The Good Stuff

    Included in the downloadable code (see the Downloads at the bottom of this article) is a Web project called TestWeb with nine (9) example pages on how to use my GMap Control in your applications (see all examples here). The download includes the complete source for the control.

    Basics.aspx

    __Show Me__ This is the simplest use of a GMap. Here, you just create a GMap with a width of 500px and height of 300px. What could be simpler?

    <form id="Form1" method="post" runat="server">
       <wcp:GMap runat="server" id="gMap" Width="500px" Height="300px" />
    </form>
    

    Basics.aspx.cs

    To initialize the map, you'll always want to make a call to CenterAndZoom. Otherwise, you may get just a big grey box.

    protected GMap gMap;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       gMap.CenterAndZoom(new GPoint(-122.141944F, 37.441944F), 4);
    }
    

    Houston, we have a GMap!

    Controls.aspx.cs

    __Show Me__ The .aspx code for the Controls example is identical to the Basics example. Here, you add some basic controls to your map.

    protected GMap gMap;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       gMap.AddControl( new GSmallMapControl());
       gMap.AddControl( new GMapTypeControl());
       gMap.CenterAndZoom(new GPoint(-122.141944F, 37.441944F), 4);
    }
    

    GMap & GMarker Events

    Say you want to tap into some of the events provided by the Google Maps API. You can access any of the GMap events by implementing the proper JavaScript functions in your page. The events are outlined below and demonstrated in the following examples:

    Event Handler Notes
    GMap_Click(overlay, point) "this" will reference the GMap being clicked
    GMap_Move() "this" will reference the GMap being moved
    GMap_MoveStart() "this" will reference the GMap being moved
    GMap_MoveEnd() "this" will reference the GMap that was moved
    GMap_Zoom(oldZoomLevel, newZoomLevel) "this" will reference the GMap being zoomed
    GMap_WindowOpen()  
    GMap_WindowClose()  
    GMap_AddOverlay(overlay) "this" will reference the GMap to which the overlay was added
    GMap_RemoveOverlay(overlay) "this" will reference the GMap that lost the overlay
    GMap_ClearOverlays() "this" will reference the GMap being cleared
    GMarker_Click() "this" will reference the GMarker that was clicked
    GMarker_InfoWindowOpen()  
    GMarker_InfoWindowClose()  

    Downloads

  • WCPierce.zip
  • WCPierceWebChm.zip

  • IT Offers


    Top Authors