Friday, January 09, 2009

Adding Bounding Box to Google Maps in ASP.NET

I used this bounding box example code: http://www.svennerberg.com/2008/11/bounding-box-in-google-maps/

And applied it to the ASP.NET control I've already blogged about here - from Jeevan Murkoth on this address - http://dotnet.sys-con.com/node/171162

Basically I added to the GMapControl class:

        private string _sBoundingBox = string.Empty;

 

        /// <summary>

        /// Method to Center and Zoom the map around a bounding box

        /// </summary>

        /// <param name="swPoint">Point at which the map should be South West</param>

        /// <param name="nePoint">Point at which the map should be North East</param>

        public void CenterAndZoomUsingBoundingBox(GPoint swPoint, GPoint nePoint)

        {

            StringBuilder boundingBuilder = new StringBuilder();

            boundingBuilder.AppendFormat("var sw{2}=new GLatLng({0},{1});", swPoint.Longitude, swPoint.Latitude, this.ClientID);

            boundingBuilder.AppendFormat("var ne{2}=new GLatLng({0},{1});", nePoint.Longitude, nePoint.Latitude, this.ClientID);

            boundingBuilder.AppendFormat("var bounds{0}=new GLatLngBounds(sw{0},ne{0});", this.ClientID);

            _sBoundingBox = boundingBuilder.ToString();

            _sZoomAndCenter = string.Empty;

        }


And I modified these GMapControl methods: 

        /// <summary>

        /// Method to Center and Zoom the map at a particular point

        /// </summary>

        /// <param name="curPoint">Point at which the map should be centered</param>

        /// <param name="ZoomLevel">Zoom level</param>

        public void CenterAndZoom(GPoint curPoint, int ZoomLevel)

        {

            _sZoomAndCenter = string.Format("ZoomIn{3}({0},{1},{2});", curPoint.Longitude, curPoint.Latitude, ZoomLevel, this.ClientID);

            _sBoundingBox = string.Empty;

        }

 

        /// <summary>

        /// Method that Generates all the Javascript

        /// </summary>

        /// <returns>Javascript code as a string</returns>

        private string InitializeMap()

        {

            //This is a work around for the thread abort error in IE

            //IE doesn't seem to like script tags in the body so close the form and

            //open another form element

            _sInitFunction.Append("</form>");

            _sInitFunction.Append("<script language='javascript'>\n");

            //Check if the Browser is compatible

            _sInitFunction.Append("if(GBrowserIsCompatible()){\n");

            _sInitFunction.AppendFormat("var {0};", _sGMapVariable);

            _sInitFunction.AppendFormat("var {0}= new Array();", _sArrayVariable);

            _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateNewOverLayFunction());

            // If pop windows are to be shown then the enable drag has to be set to true

            if (_bOverrideEnabledrag)

                _bEnabledrag = true;

            _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateGMapVariableInitialization(_sGMapVariable, this.ClientID, _bEnabledrag, _bEnableInfoWindow, _bEnableMapTypeControl));

            _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateMapType(_MapType, _sGMapVariable));

            _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateScrollControl(_MapScrollType, _sGMapVariable));

            _sInitFunction.AppendFormat("{0}\n", _sPointOverlay.ToString());

            _sInitFunction.AppendFormat("{0}\n", _sLineOverlay.ToString());

            _sInitFunction.AppendFormat("{0}\n", _sBoundingBox);

            if (_sBoundingBox.Trim().Length > 0)

                _sInitFunction.AppendFormat("{0}.centerAndZoom(bounds{1}.getCenter(),{0}.getBoundsZoomLevel(bounds{1}));", _sGMapVariable, this.ClientID);

            _sInitFunction.AppendFormat("{0}\n", _sZoomAndCenter);

            _sInitFunction.AppendFormat("{0}.addOverlays({1});\n", _sGMapVariable, _sArrayVariable);

            _sInitFunction.Append("}\n");

            //Render the function only if needed.This reduces the  client side  Javascript rendered

            if (_sZoomAndCenter.Trim().Length > 0)

                _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateZoomInFunction(_sGMapVariable, this.ClientID));

 

            if (_sPointOverlay.Length > 0)

            {

                _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateOverLayPointFuncCall(_sArrayVariable, this.ClientID));

                _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateCustomOverLayPointFuncCall(_sArrayVariable, this.ClientID));

            }

            //Render the function only if needed.This reduces the client side Javascript rendered

            if (_sLineOverlay.Length > 0)

                _sInitFunction.AppendFormat("{0}\n", myScriptGenerator.GenerateOverlayLineFuncCall(_sArrayVariable, this.ClientID));

 

            _sInitFunction.Append("</script>\n");

            _sInitFunction.Append("<form>");

            return _sInitFunction.ToString();

        }

 

 And in I modified these JScriptGenerator methods: 

 

            /// <summary>

            /// Method that generates JavaScript code to Initialize the map Variable

            /// </summary>

            /// <param name="sGMapVariable">Variable representing the map</param>

            /// <param name="sClientID">Client ID of the control</param>

            /// <param name="bEnableDrag">Set if the map can be dragged</param>

            /// <param name="bEnableInfoWindow">set if there can be pop up windows on the map</param>

            /// <param name="bEnableMapTypeControl">set if the map Type can be changed</param>

            /// <returns>JavaScript code as string</returns>

            public string GenerateGMapVariableInitialization(string sGMapVariable, string sClientID,bool bEnableDrag,bool bEnableInfoWindow, bool bEnableMapTypeControl)

            {

                  StringBuilder sInitVarBuilder= new StringBuilder();

                  sInitVarBuilder.AppendFormat("{1} =new GMap2(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();

            }

 

            /// <summary>

            /// Method to generate the Javascript function to center and Zoom in

            /// at a point on a map

            /// </summary>

            /// <param name="sGMapVariable">Variable representing the map</param>

            /// <param name="sClientID">Client ID of the control</param>

            /// <returns>JavaScript function as a string</returns>

            ///<code>

            /// function ZoomIn(clong,cLat,ZoomLevel)

            ///   {

            ///         map.centerAndZoom(new GPoint(clong,cLat), ZoomLevel);

            ///   }

            /// </code>

            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}.setCenter(new GPoint(clong,cLat),ZoomLevel)", sGMapVariable);

                  sZoomInFunction.Append("\n; }");

                  return sZoomInFunction.ToString();

            }


        /// <summary>

        /// 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

        /// </summary>

        /// <returns>JavaScript code as string</returns>

        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("GMap2.prototype.addOverlays=function(a)\n");

            sOverLayFunction.Append("{  var b=this;\n ");

            sOverLayFunction.Append(" for (i=0;i<a.length;i++)\n");

            sOverLayFunction.Append("{\n");

            // SL - changed

            //sOverLayFunction.Append("try{this.overlays.push(a[i]); \n");

            sOverLayFunction.Append("try{this.addOverlay(a[i]); \n");

            sOverLayFunction.Append("a[i].initialize(this);\n");

            sOverLayFunction.Append(" a[i].redraw(true);\n");

            sOverLayFunction.Append(" }catch(er){}\n");

            // SL - changed

            //sOverLayFunction.Append("} this.reOrderOverlays();}; ");

            sOverLayFunction.Append("} }; ");

            return sOverLayFunction.ToString();

        }


This was a lot more involved than I expected - all part of the shift from GMaps version 1 to version 2 and I've no idea what I've broken now....

2 comments:

  1. can u give a link for dwonload :)

    ReplyDelete
  2. How to make point on gmap control with asp.net 2.0

    ReplyDelete