|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is the first article in a three article series examining 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 checkout this site and this one for the low-down. You may see references throughout the article to "my control, my GoogleMaps control, my GMap control, etc.". I did not create the Google Maps API, I merely created a wrapper for ASP.NET using C#, XML, and XSL. Normally my first article would focus on creating the control. But you really don’t need to understand how the control was made to start using it. I’m really anxious to get feedback about this control so rather than make you sit through the boring "creation" article first; you get the practical stuff now. 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 and configurationI need to point out a few quick things before we get to the examples. The Google Maps Documentation makes some specific recommendations for the <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
" href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">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" Google API KeyYou’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 setupThe <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 The good stuffIncluded in the downloadable code (see Download source files above) is a web project called TestWeb with nine (9) example pages on how to use my Basics.aspx__Show Me__ This is the simplest use of a <form id="Form1" method="post" runat="server">
<wcp:GMap runat="server" id="gMap"
Width="500px" Height="300px" />
</form>
Basics.aspx.csTo initialize the map, you’ll always want to make a call to 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 Controls.aspx.cs__Show Me__ The .aspx code for the Controls example is identical to the Basics example. Here we are adding some basic controls to our 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 and GMarker eventsNow let’s say you want to tap into some of the events provided by the Google Maps API. You can access any of the
Listeners.aspx<script type="text/javascript">
function GMap_MoveEnd()
{
var center = this.getCenterLatLng();
var latLngStr =
'(' + center.y + ', ' + center.x + ')';
document.getElementById('message').innerHTML =
latLngStr;
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<wcp:GMap runat="server" id="gMap"
Width="500px" Height="300px" />
<asp:Label Runat="server" ID="message" />
</form>
</body>
Here we are implementing one of the InfoWindow.aspx.cs__Show Me__ This example opens the GMap Info Window and displays "Hello World". Note this examples uses JavaScript to create a new DOM text node. protected GMap gMap;
private void Page_Load(object sender,
System.EventArgs e)
{
GPoint gp = new GPoint(-122.141944F, 37.441944F);
gMap.CenterAndZoom(gp, 4);
gMap.OpenInfoWindow(gp,
"document.createTextNode('Hello World')");
}
Overlays.aspx.cs__Show Me__ What fun is a map without points of interest or line annotations? You can add "overlays" to your protected GMap gMap;
private void Page_Load(object sender, System.EventArgs e)
{
gMap.AddControl(new GSmallMapControl());
gMap.AddControl(new GMapTypeControl());
GPoint gp = new GPoint(-122.141944F, 37.441944F);
gMap.CenterAndZoom(gp, 5);
GMarker gm = new GMarker(gp, "FirstMarker");
gMap.Overlays.Add(gm);
GPolyline gPoly = new GPolyline();
gPoly.Weight = 5;
gPoly.Opacity = 0.25F;
gPoly.Color = Color.Red;
for( int i=1; i<6; i++ )
{
float x = gp.X + (0.005F*-i);
float y = gp.Y + (0.005F*-i);
gPoly.Points.Add(new GPoint(x, y));
}
gMap.Overlays.Add(gPoly);
}
ClickHandling.aspx__Show Me__ You can also capture the <script type="text/javascript">
function GMap_Click(overlay, point)
{
if( overlay )
this.removeOverlay(overlay);
else if( point )
this.addOverlay(new GMarker(point));
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<wcp:GMap runat="server" id="gMap"
Width="500px" Height="300px" />
</form>
</body>
MarkerInfoWindow.aspx__Show Me__ You can also capture the <script type="text/javascript">
function GMarker_Click()
{
var html = "<b>" + this.id + "</b>";
this.openInfoWindowHtml(html);
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<wcp:GMap runat="server" id="gMap"
Width="500px" Height="300px" />
</form>
</body>
MarkerInfoWindow.aspx.csCreate the protected GMap gMap;
private void Page_Load(object sender, System.EventArgs e)
{
GPoint gp = new GPoint(-122.141944F, 37.441944F);
GMarker gm = new GMarker(gp, "FirstMarker");
gMap.Overlays.Add(gm);
gm = new GMarker(new GPoint(gp.X + 0.005F,
gp.Y + 0.005F), "SecondMarker");
gMap.Overlays.Add(gm);
gMap.CenterAndZoom(gp, 4);
}
Icons.aspx__Show Me__ The <script type="text/javascript">
function GMarker_Click()
{
var html = "You clicked me!";
this.openInfoWindowHtml(html);
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<wcp:GMap runat="server" id="gMap"
Width="500px" Height="300px" />
</form>
</body>
Icons.aspx.csAgain, the theme here is programmatically creating Google API objects and interacting with them using C#/VB.NET. protected GMap gMap;
private void Page_Load(object sender, System.EventArgs e)
{
GPoint gp = new GPoint(-122.141944F, 37.441944F);
GIcon tiny = new GIcon();
tiny.Id = "tiny";
tiny.Image = new Uri(
"http://labs.google.com/ridefinder/images/mm_20_red.png");
tiny.Shadow = new Uri(
"http://labs.google.com/ridefinder/images/mm_20_shadow.png");
tiny.IconSize = new GSize(12, 20);
tiny.ShadowSize = new GSize(22, 20);
tiny.IconAnchor = new GPoint(6, 20);
tiny.InfoWindowAnchor = new GPoint(5, 1);
gMap.Icons.Add(tiny);
gMap.AddControl(new GSmallMapControl());
gMap.AddControl(new GMapTypeControl());
gMap.CenterAndZoom(gp, 4);
GMarker gm = new GMarker(gp, "TinyMarker", tiny.Id);
gMap.Overlays.Add(gm);
}
DataListExample.aspx__Show Me__ This is the final example that I wanted to throw into this article. I wanted to demonstrate just how easily you could create multiple <body>
<form id="Form1" method="post" runat="server">
<asp:DataList Runat="server" ID="dlMaps"
RepeatDirection="Horizontal"
RepeatColumns="2" RepeatLayout="Table">
<ItemTemplate>
<wcp:GMap runat="server" ID="gMap"
width="400px" height="240px" />
<asp:Label Runat="server">
Location: (-122.141944, 37.441944) Zoom Level:
<%# Container.DataItem %>
</asp:Label>
</ItemTemplate>
</asp:DataList>
</form>
</body>
DataListExample.aspx.csHere we get a little fancy by creating an protected DataList dlMaps;
private const int MAX_ZOOM = 6;
private void Page_Load(object sender,
System.EventArgs e)
{
ArrayList al = new ArrayList();
for( int i=1; i<=MAX_ZOOM; i++ )
al.Add(i);
dlMaps.DataSource = al;
dlMaps.DataBind();
}
private void dlMaps_ItemDataBound(object s,
DataListItemEventArgs dliea)
{
GMap map = (GMap)dliea.Item.Controls[0];
int zoom = Convert.ToInt32(dliea.Item.DataItem);
map.CenterAndZoom(new GPoint(-122.141944F,
37.441944F), zoom);
}
ConclusionIn this article we covered the basic usage of my .NET
In Part 3 I will discuss how I designed the | ||||||||||||||||||||||||||||||||||||||||||||||||||