|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is the second part in a two part series of my article, Google Maps User Control for ASP.NET. In the first part, Google Maps Control for ASP.NET - Part 1, I have explained how to use this control in your ASP.NET application. In this part, I am going to explain the source code of this user control so that you can modify it for your own use. DiagramThe diagram above shows the working flow of this user control. I will explain to you one by one each element in this diagram. ASPX page with the Google Map User Control
protected void Page_Load(object sender, EventArgs e)
{
//Specify API key
GoogleMapForASPNet1.GoogleMapObject.APIKey =
ConfigurationManager.AppSettings["GoogleAPIKey"];
//Specify width and height for map.
GoogleMapForASPNet1.GoogleMapObject.Width = "800px";
// You can also specify percentage(e.g. 80%) here
GoogleMapForASPNet1.GoogleMapObject.Height = "600px";
//Specify initial Zoom level.
GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 14;
//Specify Center Point for map.
GoogleMapForASPNet1.GoogleMapObject.CenterPoint =
new GooglePoint("1", 43.66619, -79.44268);
//Add pushpins for map.
//This should be done with intialization of GooglePoint class.
//In constructor of GooglePoint, First argument is ID of this pushpin.
//It must be unique for each pin. Type is string.
//Second and third arguments are latitude and longitude.
GoogleMapForASPNet1.GoogleMapObject.Points.Add(
new GooglePoint("1", 43.65669, -79.45278));
}
GOOGLE_MAP_OBJECT session variable. Later on, this session variable is accessed by the GService.asmx web service to draw the Google map.Page_Load() method.protected void Page_Load(object sender, EventArgs e)
{
.
.
.
if (!IsPostBack)
{
Session["GOOGLE_MAP_OBJECT"] = GoogleMapObject;
}
else
{
GoogleMapObject = (GoogleObject)Session["GOOGLE_MAP_OBJECT"];
.
.
.
As you can see, I am storing the User Control (GoogleMapForASPNet.ascx)
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"onLoadCall", "<script language="'javascript'"> " +
"if (window.DrawGoogleMap) { DrawGoogleMap(); } </script>");
This causes the GoogleMapAPIWrapper.js
Web Service (GService.asmx)
How to define a Web Service method
[WebMethod(EnableSession = true)]
public GoogleObject GetGoogleObject()
{
GoogleObject objGoogle =
(GoogleObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] =
new GoogleObject(objGoogle);
return objGoogle;
}
Return the value type from this method as How to call an ASP.NET function (Web Service method) from JavaScript using a Web Service
<asp:ScriptManagerProxy ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/GService.asmx" />
</Services>
</asp:ScriptManagerProxy>
When DrawGoogleMap() function.function DrawGoogleMap()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("GoogleMap_Div"));
geocoder = new GClientGeocoder();
GService.GetGoogleObject(fGetGoogleObject);
}
}
GService.GetGoogleObject() is a web service method. fGetGoogleObject() is a javascript function that should be called once web service method returns. function fGetGoogleObject(result, userContext)
{
map.setCenter(new GLatLng(result.CenterPoint.Latitude,
result.CenterPoint.Longitude), result.ZoomLevel);
if(result.ShowMapTypesControl)
{
map.addControl(new GMapTypeControl());
}
.
.
.
The Difference between GetGoogleObject() and GetGoogleObjectOptimized()
function endRequestHandler(sender, args)
{
GService.GetOptimizedGoogleObject(fGetGoogleObjectOptimized);
}
function pageLoad()
{
if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}
These functions cause the Functions defined in GoogleMapAPIWrapper.jsTo make this article short, I don't want to explain each and every function defined in this file. Instead, I will explain the important functions only. If you want more details of any code that's not explained here, you can post your questions in the Comments section.. function CreateMarker(point,icon1,InfoHTML,bDraggable,sTitle)
{
var marker;
marker = new GMarker(point,{icon:icon1,draggable:bDraggable,title: sTitle});
if(InfoHTML!='')
{
GEvent.addListener(marker, "click",
function() { this.openInfoWindowHtml(InfoHTML); });
}
GEvent.addListener(marker, "dragend", function() {
GService.SetLatLon(this.value,this.getLatLng().y,this.getLatLng().x);
RaiseEvent('PushpinMoved',this.value); });
return marker;
}
This function creates a marker on the Google map. As you can see, I am adding two events with each marker. The first one is function RaiseEvent(pEventName,pEventValue)
{
document.getElementById('<%=hidEventName.ClientID %>').value = pEventName;
document.getElementById('<%=hidEventValue.ClientID %>').value = pEventValue;
__doPostBack('UpdatePanel1','');
}
When the postback finishes, the new latitude and longitude values will be picked up from the Session variable. function RecenterAndZoom(bRecenter,result)
This function causes re-centering of the map. It finds all the visible markers on the map and decides the center point and the zoom level based on these markers. function CreatePolyline(points,color,width,isgeodesic)
This function creates polylines between the given points. See the function CreatePolygon(points,strokecolor,strokeweight,
strokeopacity,fillcolor,fillopacity)
This function creates polygons between the given points. See the How to define icons for the Google map
.
.
myIcon_google = new GIcon(G_DEFAULT_ICON);
markerOptions = { icon:myIcon_google };
myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth,
result.Points[i].IconImageHeight);
myIcon_google.image = result.Points[i].IconImage;
myIcon_google.shadow = result.Points[i].IconShadowImage;
myIcon_google.shadowSize = new GSize(result.Points[i].IconShadowWidth,
result.Points[i].IconShadowHeight);
myIcon_google.iconAnchor = new GPoint(result.Points[i].IconAnchor_posX,
result.Points[i].IconAnchor_posY);
myIcon_google.infoWindowAnchor = new GPoint(result.Points[i].InfoWindowAnchor_posX,
result.Points[i].InfoWindowAnchor_posY);
.
.
Special notesI have published this article on my blog as well. Here is the link to the article: Google Maps Control for ASP.NET - Part 2.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||