Tuesday, December 09, 2008

JScriptGenerator.cs with fixes for Gmap2.0

As I mentioned yesterday, I've put some fixes into JScriptGenerator.cs from http://dotnet.sys-con.com/node/171162

All changes are marked "// SL" - there aren't many. Here they are:



using System;
using System.Text;
namespace MapControl
{
///
/// Class that handles all Javascript Method
/// Generation
///
internal class JScriptGenerator
{
///
/// Default constructor
///
public JScriptGenerator()
{

}
///
/// Method to Generate the Function to Overlay a point
/// on the map
///
/// Variable representing an Array
/// Client ID of the control
/// JavaScript function as a string
public string GenerateOverLayPointFuncCall(string sArrayVariable, string sControlClientID)
{
StringBuilder sOverLayFunction = new StringBuilder();
sOverLayFunction.AppendFormat(" function OverlayLocation{0}(clong,cLat,popup,sFormattedHtml)\n",sControlClientID );
sOverLayFunction.Append("{\n");
sOverLayFunction.Append("var point = new GPoint(clong,cLat);");
sOverLayFunction.Append("var marker = new GMarker(point);\n");
sOverLayFunction.Append("if ( popup==1 ) { \n");
sOverLayFunction.Append("GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(sFormattedHtml);});");
sOverLayFunction.Append("\n } \n");
sOverLayFunction.AppendFormat("{0}.push(marker);\n",sArrayVariable);
sOverLayFunction.Append(" }");
return sOverLayFunction.ToString();

}
///
/// Method to Generate the Javascript funtion to Overlay a point that uses a custom
/// icon on the map
///
/// Variable representing an Array
/// Client ID of the control
/// JavaScript function as a string
public string GenerateCustomOverLayPointFuncCall(string sArrayVariable, string sControlClientID)
{
StringBuilder sOverLayFunction = new StringBuilder();
sOverLayFunction.AppendFormat(" function OverlayCustomMarkerLocation{0}(clong,cLat,iconimgUrl,iconshadowimgURL,iconwidth,iconheight,iconshwidth,iconshheight,iconAnchorX,iconAnchorY,infoWinX,infoWinY,popup,sFormattedHtml)\n",sControlClientID );
sOverLayFunction.Append("{\n");
sOverLayFunction.Append("var point = new GPoint(clong,cLat);\n");
sOverLayFunction.Append("var icon = new GIcon();\n");
sOverLayFunction.Append("icon.image =iconimgUrl ;\n");
sOverLayFunction.Append("icon.shadow =iconshadowimgURL ;\n");
sOverLayFunction.Append("icon.iconSize = new GSize(iconwidth, iconheight);\n");
sOverLayFunction.Append("icon.shadowSize = new GSize(iconshwidth, iconshheight);\n");
sOverLayFunction.Append("icon.iconAnchor = new GPoint(iconAnchorX, iconAnchorY);\n");
sOverLayFunction.Append("icon.infoWindowAnchor = new GPoint(infoWinX, infoWinY);\n");
sOverLayFunction.Append("var marker = new GMarker(point,icon);\n");
sOverLayFunction.Append("if ( popup==1 ) { \n");
sOverLayFunction.Append("GEvent.addListener(marker, 'click', function() {");
sOverLayFunction.Append("marker.openInfoWindowHtml(sFormattedHtml);});");
sOverLayFunction.Append(" } \n");
sOverLayFunction.AppendFormat("{0}.push(marker);\n",sArrayVariable);
sOverLayFunction.Append(" }");
return sOverLayFunction.ToString();

}

///
/// Method to generate the Javascript function to overlay a line
/// on the map
///
/// Variable representing the array
/// Client ID of the control
/// JavaScript function as a string
public string GenerateOverlayLineFuncCall(string sArrayVariable, string sControlClientID)
{
StringBuilder sOverLayFunction = new StringBuilder();
sOverLayFunction.AppendFormat(" function OverlayLine{0}(PointArray,weight,Color,opacity)\n",sControlClientID );
sOverLayFunction.Append("{\n");
sOverLayFunction.Append("var polyline = new GPolyline(PointArray,Color,weight,opacity);");
sOverLayFunction.AppendFormat("{0}.push(polyline);\n",sArrayVariable);
sOverLayFunction.Append(" }");
return sOverLayFunction.ToString();
}
///
/// Method to Overlay points on the map
/// Instead of overlaying each point or lay individually
/// it stores them in an array and overlays them together
///
/// JavaScript code as string
public string GenerateNewOverLayFunction()
{
// The credit for the method goes to Lokkju and is available
// on the google group for Google Maps
StringBuilder sOverLayFunction = new StringBuilder();
sOverLayFunction.Append("GMap.prototype.addOverlays=function(a)\n");
sOverLayFunction.Append("{ var b=this;\n ");
sOverLayFunction.Append(" for (i=0;iVariable representing the map
/// Client ID of the control
/// JavaScript function as a string
///
/// function ZoomIn(clong,cLat,ZoomLevel)
/// {
/// map.centerAndZoom(new GPoint(clong,cLat), ZoomLevel);
/// }
///
public string GenerateZoomInFunction(string sGMapVariable ,string sClientID)
{
StringBuilder sZoomInFunction = new StringBuilder();
//Generate a unique Name for the function
sZoomInFunction.AppendFormat("function ZoomIn{0}(clong,cLat,ZoomLevel)",sClientID);
sZoomInFunction.Append("\n{");
sZoomInFunction.AppendFormat("{0}.centerAndZoom(new GPoint(clong,cLat),ZoomLevel)",sGMapVariable );
sZoomInFunction.Append("\n; }");
return sZoomInFunction.ToString();
}
///
/// Utility Method to generate JavaScript code to Set the Map type
///
/// The Type of Map
/// Variable representing the Google Map object
/// Javascript code as string
public string GenerateMapType(GMapType mapType,string sGMapVariable)
{
string sMapType=string.Empty;
switch(mapType)
{
case GMapType.MAP :
// SL - changed
sMapType = "G_NORMAL_MAP";
break;
case GMapType.HYBRID:
// SL - changed
sMapType = "G_HYBRID_MAP";
break;
case GMapType.SATELLITE :
// SL - changed
sMapType = "G_SATELLITE_MAP";
break;
default:
// SL - changed
sMapType = "G_NORMAL_MAP";
break;
}
return string.Format("{0}.setMapType({1});\n",sGMapVariable,sMapType);
}
///
/// Method that generates JavaScript code to Initialize the map Variable
///
/// Variable representing the map
/// Client ID of the control
/// Set if the map can be dragged
/// set if there can be pop up windows on the map
/// set if the map Type can be changed
/// JavaScript code as string
public string GenerateGMapVariableInitialization(string sGMapVariable, string sClientID,bool bEnableDrag,bool bEnableInfoWindow, bool bEnableMapTypeControl)
{
StringBuilder sInitVarBuilder= new StringBuilder();
sInitVarBuilder.AppendFormat("{1} =new GMap(document.getElementById('{0}'));\n",sClientID,sGMapVariable);
if(!bEnableDrag)
sInitVarBuilder.AppendFormat("{0}.disableDragging() ;\n",sGMapVariable);
if(!bEnableInfoWindow)
sInitVarBuilder.AppendFormat("{0}.disableInfoWindow() ;\n",sGMapVariable);
if( bEnableMapTypeControl)
sInitVarBuilder.AppendFormat("{0}.addControl(new GMapTypeControl()) ;\n",sGMapVariable);
return sInitVarBuilder.ToString();
}
///
/// Method to Generat the JavaScript code to set the type of
/// Scroll Control on the Map
///
/// The Scroll Control to be added
/// Variable representing the Google Map
/// Javascript code as string
public string GenerateScrollControl(GMapScrollControl scrollType, string sGmapVariable)
{
string sScrollType=string.Empty;
switch (scrollType)
{
case GMapScrollControl.LARGE:
sScrollType= string.Format(" {0}.addControl(new GLargeMapControl());\n",sGmapVariable);
break;
case GMapScrollControl.SMALL :
sScrollType=string.Format(" {0}.addControl(new GSmallMapControl());\n",sGmapVariable);
break;
case GMapScrollControl.SMALLZOOMONLY:
sScrollType=string.Format(" {0}.addControl(new GSmallZoomControl());\n",sGmapVariable);
break;
case GMapScrollControl.NONE:
default:
sScrollType=string.Empty;
break;

}
return sScrollType;
}

}
}

No comments:

Post a Comment