Set the zoom level programmatically

Jul 14, 2011 at 6:22 PM

Is there a way to set the zoom level programmatically based on the min and max lat longs of the markers being displayed?

Jul 15, 2011 at 2:28 PM


You need to inject the javascript to do it.  Here is an example where that also sets the bounds:
  1. ndMapZoom is the desired Zoom as integer
  2. sbZoomMapScript is an instantiated StringBuilder
  3. objCurPinSet is the set of locations with the min/max lat/long figured out
   If ndMapZoom < 0 Then

                    '\\ Set Zoom with JavaScript based on Min/Max Lat/Longs

                    With sbZoomMapScript
                        .AppendFormat("var sw = new GLatLng({0}, {1});", objCurPinSet.MinimumLatitude, objCurPinSet.MinimumLongitude).AppendLine()
                        .AppendFormat("var ne = new GLatLng({0}, {1});", objCurPinSet.MaximumLatitude, objCurPinSet.MaximumLongitude).AppendLine()
                        .AppendFormat("var bounds = new GLatLngBounds(sw, ne);").AppendLine()
                        .AppendFormat("var zoomLevel = {0}.GMap.getBoundsZoomLevel(bounds);", GoogleMap1.ClientID).AppendLine()
                        .AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap1.ClientID).AppendLine()
                    End With


                    With sbZoomMapScript
                        .AppendFormat("var zoomLevel = " & ndMapZoom & ";", GoogleMap1.ClientID).AppendLine()
                        .AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap1.ClientID).AppendLine()
                    End With

                End If

Me.GoogleMap1.OnClientMapLoad &= sbZoomMapScript.ToString



Jul 16, 2011 at 1:21 AM

I added this to my setup:


StringBuilder javaScriptZoomString = new StringBuilder();

        javaScriptZoomString.AppendFormat("var sw = new GLatLng({0}, {1});", minLat, maxLong).AppendLine();
        javaScriptZoomString.AppendFormat("var ne = new GLatLng({0}, {1});", maxLat, maxLong).AppendLine();
        javaScriptZoomString.AppendFormat("var bounds = new GLatLngBounds(sw, ne);").AppendLine();
        javaScriptZoomString.AppendFormat("var zoomLevel = {0}.GMap.getBoundsZoomLevel(bounds);", GoogleMap.ClientID).AppendLine();
        javaScriptZoomString.AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap.ClientID).AppendLine();

        this.GoogleMap.OnClientMapLoad += javaScriptZoomString.ToString();


When this runs however, the map doesnt display the correct default view i had set and it also doesnt display my markers.  When i take out the last line, it shows up but the zoom level is wrong...  Any ideas?

Jul 16, 2011 at 1:01 PM

You put the code where?  I have it in the page-init.  I don't have time to test yours now, but generally, my experience has been if the map doesn't show at all, its because the javascript is erroring out - using google chrome's debugging tools can help enormously!

Jul 18, 2011 at 5:56 PM

I have it in a function that creates a bunch of markers and loads it on the map. That function gets called by the page_load function.  So i'm assuming its because the OnClientMapLoad event happens before my function is being called. 

Jul 18, 2011 at 5:59 PM

You are probably correct - the sample I gave you also creates many custom markers, but I do it all in Page_Init!

Feb 13, 2012 at 11:39 AM

There is no onclientmapload event at v6. where can i bind it instead ?

Apr 6, 2012 at 9:25 PM

The code TMCentral posted is now completely broken in the new version.  Here is my solution that has worked for me perfectly so far (also C# version):


            int ndMapZoom = -5;
            StringBuilder sbZoomMapScript = new StringBuilder();

            sbZoomMapScript.Append("function initZoom() {");
            if(ndMapZoom < 0){

                   // Set Zoom with JavaScript based on Min/Max Lat/Longs

                        .AppendFormat("var sw = new google.maps.LatLng({0}, {1});", minLat, minLng).AppendLine()
                        .AppendFormat("var ne = new google.maps.LatLng({0}, {1});", maxLat, maxLng).AppendLine()
                        .AppendFormat("var bounds = new google.maps.LatLngBounds();").AppendLine()
                        .Append("bounds.extend(sw); bounds.extend(ne);")
                        .AppendFormat("maps.{0}.fitBounds(bounds);", propertyMap.ClientID).AppendLine();

                        .AppendFormat("var zoomLevel = " + ndMapZoom + ";", propertyMap.ClientID).AppendLine()
                        .AppendFormat("maps.{0}.setZoom(zoomLevel);", propertyMap.ClientID).AppendLine();

            sbZoomMapScript.Append(@"function checkInit(){
                                        if(typeof maps === 'undefined'){
                                            setTimeout('checkInit()', 500);

            ClientScript.RegisterClientScriptBlock(typeof(string), "zoomctrl", sbZoomMapScript.ToString(), true);


Nov 21, 2012 at 2:53 AM

This worked for me


searchResultsMap.Bounds = new Bounds();           

searchResultsMap.Bounds.NorthEast = new LatLng(latList.Max(), longList.Max());

searchResultsMap.Bounds.SouthWest = new LatLng(latList.Min(), longList.Min());


where latList is a list with all the marker latitudes and longList is a list with all the marker longitudes