How To Save the Zoom View of Our Map in ASP.NET

Introduction
 
Hi all, I hope you all are fine. Today we will learn how to save the zoom level of our map in our application. By this you can retain the zoomed view that the user sets so that it would be easier for users to identify things in our application.
 
Please see this article in my blog 
 
Background
 
A few weeks ago I got the requirement to save the zoom level of the map. If you are new to the Bubble Map, please see here.
Using the code
 
I hope you have configured a Bubble Map in your application now. Now it is time to complete our new requirement. Now what we will do is to introduce a new function that retains the zoom level from the session storage and applies it to the map. You must call this function at the end of the map configuration. 
 
The following is the function code.
  1. var curMax = ''  
  2.         var curMin = ''  
  3.         var curMaxY = '';  
  4.         var curMinY = '';         
  5.         function updateZoom(chart) {  
  6.             if (sessionStorage.getItem("curMaxZoomMap") != null && typeof sessionStorage.getItem("curMaxZoomMap") != 'undefined' && sessionStorage.getItem("curMinZoomMap") != null && typeof sessionStorage.getItem("curMinZoomMap") != 'undefined') {  
  7.                 chart.xAxis[0].setExtremes(parseInt(sessionStorage.getItem("curMinZoomMap")), parseInt(sessionStorage.getItem("curMaxZoomMap")));  
  8.             }  
  9.             if (sessionStorage.getItem("curMaxZoomMapY") != null && typeof sessionStorage.getItem("curMaxZoomMapY") != 'undefined' && sessionStorage.getItem("curMinZoomMapY") != null && typeof sessionStorage.getItem("curMinZoomMapY") != 'undefined') {  
  10.                 chart.yAxis[0].setExtremes(parseInt(sessionStorage.getItem("curMinZoomMapY")), parseInt(sessionStorage.getItem("curMaxZoomMapY")));  
  11.             }  
  12.         }  
Now we have seen how to apply the zoom level to the map. Next is how to set the value to the session storage. The fact is that whenever the user does zooming in the map, it fires events in xAxis and yAxis. So we need to crate that events in both axis.
 
Let's look at that code now.

xAxis            
  1. xAxis: {  
  2.          events: {  
  3.             afterSetExtremes: function (e) {  
  4.                  curMax = e.max;  
  5.                  curMin = e.min;  
  6.                  sessionStorage.setItem("curMaxZoomMap", curMax);  
  7.                  sessionStorage.setItem("curMinZoomMap", curMin);  
  8.              }  
  9.          }  
  10. },  

yAxis
  1. yAxis: {  
  2.           events: {  
  3.               afterSetExtremes: function (e) {  
  4.                       curMaxY = e.max;  
  5.                       curMinY = e.min;  
  6.                       sessionStorage.setItem("curMaxZoomMapY", curMaxY);  
  7.                       sessionStorage.setItem("curMinZoomMapY", curMinY);  
  8.                }  
  9.            }  
  10. },  
Once you have included this event you can see the values in session storage in your browser whenever the user zooms the map.
 


Figure 1 Map
 
Our next step is to include the zoomTo even in the series of our map. 
  1. events: {  
  2.             click: function (e) {  
  3.                e.point.zoomTo();  
  4.             }  
  5.         },  
 So your series configuration will be as follows.
  1. series: [{  
  2.              mapData: map,  
  3.              borderColor: '#E0E0E0',  
  4.              colour: '#E0E0E0',  
  5.              showInLegend: false,  
  6.              enableMouseTracking: true,  
  7.              cursor: 'pointer',  
  8.              dataLabels: {  
  9.                 enabled: true,  
  10.                 format: '{point.properties.postal-code}',  
  11.                 textDecoration: 'none',  
  12.                 style: { "color""darkgrey""fontWeight""bold""HcTextStroke""0px rgba(0,0,0,0.5)" }  
  13.          },  
  14.          events: {  
  15.             click: function (e) {  
  16.                e.point.zoomTo();  
  17.             }  
  18.         },  
  19. },  
That is all guys, you can retain the zoom level of your map even if you reload. Cool!
 
Complete Code
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Bubble Map Demo - SibeeshPassion</title>  
  5.     <script src="Scripts/jquery-1.11.1.min.js"></script>  
  6.     <script src="Scripts/highmaps.js"></script>  
  7.     <script src="Scripts/proj4.js"></script>  
  8.     <script src="Scripts/us-all.js"></script>  
  9.     <script src="Scripts/world.js"></script>  
  10.   
  11.     <script>  
  12.         var curMax = ''  
  13.         var curMin = ''  
  14.         var curMaxY = '';  
  15.         var curMinY = '';         
  16.         function updateZoom(chart) {  
  17.             if (sessionStorage.getItem("curMaxZoomMap") != null && typeof sessionStorage.getItem("curMaxZoomMap") != 'undefined' && sessionStorage.getItem("curMinZoomMap") != null && typeof sessionStorage.getItem("curMinZoomMap") != 'undefined') {  
  18.                 chart.xAxis[0].setExtremes(parseInt(sessionStorage.getItem("curMinZoomMap")), parseInt(sessionStorage.getItem("curMaxZoomMap")));  
  19.             }  
  20.             if (sessionStorage.getItem("curMaxZoomMapY") != null && typeof sessionStorage.getItem("curMaxZoomMapY") != 'undefined' && sessionStorage.getItem("curMinZoomMapY") != null && typeof sessionStorage.getItem("curMinZoomMapY") != 'undefined') {  
  21.                 chart.yAxis[0].setExtremes(parseInt(sessionStorage.getItem("curMinZoomMapY")), parseInt(sessionStorage.getItem("curMaxZoomMapY")));  
  22.             }  
  23.         }  
  24.         $(function () {  
  25.             loadMap();  
  26.             $('#apply').click(function () {  
  27.                 loadMap();  
  28.             });  
  29.         });  
  30.         function loadMap() {  
  31.             var selectMap = $.trim($("#mapSelectMap").val());  
  32.             var zPoint = "Quantity";  
  33.             var jsdata = [{ "State""Arizona""lat""32.8796""lon""-111.7282""Quantity""29,248" }, { "State""Arizona""lat""33.3197""lon""-111.9837""Quantity""102,928" }, { "State""Arizona""lat""33.3939""lon""-111.6965""Quantity""108,863" }, { "State""Arizona""lat""33.4495""lon""-112.3410""Quantity""67,218" }, { "State""Arizona""lat""33.6155""lon""-111.9263""Quantity""66,556" }, { "State""Arizona""lat""33.6384""lon""-112.2073""Quantity""147,552" }, { "State""California""lat""33.6145""lon""-117.7101""Quantity""26,053" }, { "State""California""lat""33.7008""lon""-117.9362""Quantity""26,057" }, { "State""California""lat""33.7963""lon""-116.3845""Quantity""497,249" }, { "State""California""lat""33.8274""lon""-118.0868""Quantity""32,052" }, { "State""California""lat""33.8763""lon""-117.7418""Quantity""23,521" }, { "State""California""lat""33.8996""lon""-118.3669""Quantity""26,169" }, { "State""California""lat""34.0302""lon""-118.3865""Quantity""21,996" }, { "State""California""lat""34.0569""lon""-117.2995""Quantity""59,135" }, { "State""California""lat""34.0737""lon""-117.8859""Quantity""4,757" }, { "State""California""lat""34.1820""lon""-118.3140""Quantity""28,909" }, { "State""California""lat""34.2257""lon""-119.1551""Quantity""22,569" }, { "State""California""lat""34.2300""lon""-118.5500""Quantity""22,413" }, { "State""California""lat""34.4130""lon""-118.5036""Quantity""18,399" }, { "State""California""lat""34.4813""lon""-117.3389""Quantity""23,268" }, { "State""California""lat""34.6029""lon""-118.1464""Quantity""24,651" }, { "State""California""lat""37.5038""lon""-121.9762""Quantity""4" }, { "State""California""lat""38.3525""lon""-122.7148""Quantity""2" }, { "State""Colorado""lat""38.4766""lon""-107.8812""Quantity""3,294" }, { "State""Colorado""lat""39.0730""lon""-108.5710""Quantity""8,704" }, { "State""Florida""lat""27.8400""lon""-82.6800""Quantity""75,066" }, { "State""Florida""lat""27.9400""lon""-82.3200""Quantity""135,365" }, { "State""Florida""lat""27.9600""lon""-82.3200""Quantity""-6" }, { "State""Florida""lat""27.9925""lon""-82.7296""Quantity""27,274" }, { "State""Florida""lat""28.0661""lon""-82.5688""Quantity""261" }, { "State""Florida""lat""28.0790""lon""-80.6984""Quantity""15,720" }, { "State""Florida""lat""28.0941""lon""-81.9738""Quantity""85,437" }, { "State""Florida""lat""28.1918""lon""-82.3812""Quantity""97,233" }, { "State""Florida""lat""28.3027""lon""-81.4216""Quantity""135,193" }, { "State""Florida""lat""28.5622""lon""-81.2074""Quantity""120,424" }, { "State""Florida""lat""28.6629""lon""-81.4176""Quantity""110,185" }, { "State""Florida""lat""29.1800""lon""-81.0800""Quantity""66,725" }, { "State""Georgia""lat""32.8100""lon""-83.7100""Quantity""3" }, { "State""Georgia""lat""33.3671""lon""-84.7647""Quantity""87,508" }, { "State""Georgia""lat""33.4600""lon""-84.2100""Quantity""155,811" }, { "State""Georgia""lat""33.4759""lon""-82.0709""Quantity""10,875" }, { "State""Georgia""lat""33.5600""lon""-84.5400""Quantity""17,937" }, { "State""Georgia""lat""33.5610""lon""-84.3223""Quantity""203,297" }, { "State""Georgia""lat""33.6810""lon""-84.1604""Quantity""134,700" }, { "State""Georgia""lat""33.8200""lon""-84.3600""Quantity""122,120" }, { "State""Georgia""lat""33.9168""lon""-83.4439""Quantity""84,648" }, { "State""Georgia""lat""33.9522""lon""-84.1343""Quantity""105,862" }, { "State""Georgia""lat""34.0051""lon""-84.5752""Quantity""-14" }, { "State""Georgia""lat""34.0076""lon""-84.5769""Quantity""204,888" }, { "State""Georgia""lat""34.0600""lon""-83.9800""Quantity""104,291" }, { "State""Georgia""lat""34.1900""lon""-84.1200""Quantity""186,459" }, { "State""Idaho""lat""42.9101""lon""-112.4605""Quantity""5,524" }, { "State""Idaho""lat""43.6048""lon""-116.3496""Quantity""14,476" }, { "State""Illinois""lat""41.5300""lon""-87.8500""Quantity""54,989" }, { "State""Illinois""lat""41.7200""lon""-88.0300""Quantity""72,366" }, { "State""Illinois""lat""42.0500""lon""-87.8300""Quantity""59,164" }, { "State""Illinois""lat""42.1000""lon""-87.9700""Quantity""49,074" }, { "State""Illinois""lat""42.1300""lon""-88.3300""Quantity""77,454" }, { "State""Illinois""lat""42.2300""lon""-87.9300""Quantity""51,251" }, { "State""Illinois""lat""42.2600""lon""-88.9700""Quantity""57,571" }, { "State""Indiana""lat""39.6292""lon""-86.1252""Quantity""24,227" }, { "State""Indiana""lat""39.6303""lon""-86.1249""Quantity""1,074" }, { "State""Indiana""lat""39.7613""lon""-86.3488""Quantity""12,319" }, { "State""Indiana""lat""39.7634""lon""-86.3489""Quantity""1,027" }, { "State""Indiana""lat""40.0028""lon""-86.1227""Quantity""901" }, { "State""Indiana""lat""41.4900""lon""-87.4700""Quantity""83,677" }, { "State""Kentucky""lat""38.1438""lon""-85.6751""Quantity""-85" }];  
  34.             var data1 = [];  
  35.             var tmp = '';  
  36.             var formatedVal = 0;  
  37.             if (selectMap == "0") {  
  38.                 var H = Highcharts,  
  39.                              map = H.maps['countries/us/us-all'],  
  40.                                 chart;  
  41.             }  
  42.             else {  
  43.                 var H = Highcharts,  
  44.                                   map = H.maps[selectMap],  
  45.                                      chart;  
  46.             }  
  47.             $.each(jsdata, function (ix, entry) {  
  48.                 if (Object.keys(entry).length >= 4) {  
  49.                     tmp = entry[zPoint].replace(/\,/g, '').replace(/\$/g, '').replace(/\%/g, '').replace(/\(/g, '').replace(/\)/g, '');  
  50.                     if (isNaN(tmp)) {  
  51.                         entry.z = entry[zPoint];  
  52.                     }  
  53.                     else {  
  54.   
  55.                     }  
  56.                     data1.push(entry);  
  57.                 }  
  58.             });  
  59.             chart = $('#container').highcharts('Map', {  
  60.                 chart: {  
  61.                     borderWidth: 1  
  62.                 },  
  63.                 title: {  
  64.                     text: 'Bubble map'  
  65.                 },  
  66.                 subtitle: {  
  67.                     text: 'Bubble map sub title'  
  68.                 },  
  69.   
  70.                 tooltip: {  
  71.                     pointFormat: '{point.State}: {point.Quantity}'  
  72.                 },  
  73.   
  74.                 mapNavigation: {  
  75.                     enabled: true,  
  76.                     buttonOptions: {  
  77.                         verticalAlign: 'bottom'  
  78.                     }  
  79.                 },  
  80.                 xAxis: {  
  81.                     events: {  
  82.                         afterSetExtremes: function (e) {  
  83.                             curMax = e.max;  
  84.                             curMin = e.min;  
  85.                             sessionStorage.setItem("curMaxZoomMap", curMax);  
  86.                             sessionStorage.setItem("curMinZoomMap", curMin);  
  87.                         }  
  88.                     }  
  89.                 },  
  90.                 yAxis: {  
  91.                     events: {  
  92.                         afterSetExtremes: function (e) {  
  93.                             curMaxY = e.max;  
  94.                             curMinY = e.min;  
  95.                             sessionStorage.setItem("curMaxZoomMapY", curMaxY);  
  96.                             sessionStorage.setItem("curMinZoomMapY", curMinY);  
  97.                         }  
  98.                     }  
  99.                 },  
  100.                 legend: {  
  101.                     enabled: true,  
  102.                     title: {  
  103.                         style: {  
  104.                             color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'  
  105.                         }  
  106.                     },  
  107.                     layout: 'vertical',  
  108.                     align: 'right',  
  109.                     verticalAlign: 'bottom',  
  110.                     floating: true,  
  111.                     valueDecimals: 0,  
  112.                     valueSuffix: '$',  
  113.                     backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'rgba(255, 255, 255, 0.85)',  
  114.                     symbolRadius: 0,  
  115.                     symbolHeight: 14  
  116.                 },  
  117.                 series: [{  
  118.                     mapData: map,  
  119.                     borderColor: '#E0E0E0',  
  120.                     colour: '#E0E0E0',  
  121.                     showInLegend: false,  
  122.                     enableMouseTracking: true,  
  123.                     cursor: 'pointer',  
  124.                     dataLabels: {  
  125.                         enabled: true,  
  126.                         format: '{point.properties.postal-code}',  
  127.                         textDecoration: 'none',  
  128.                         style: { "color""darkgrey""fontWeight""bold""HcTextStroke""0px rgba(0,0,0,0.5)" }  
  129.                     },  
  130.                     events: {  
  131.                         click: function (e) {  
  132.                             e.point.zoomTo();  
  133.                         }  
  134.                     },  
  135.                 },  
  136.                 {  
  137.                     type: 'mapbubble',  
  138.                     color: '#E0E0E0',  
  139.                     dataLabels: {  
  140.                         enabled: true,  
  141.                         formatter: function () {  
  142.                         }  
  143.                     },  
  144.                     enableMouseTracking: true,  
  145.                     joinBy: ['iso-a2''code'],  
  146.                     name: zPoint,  
  147.                     data: data1,  
  148.                     minSize: $("#mapMinBubSize").val(),  
  149.                     maxSize: $("#mapMaxBubSize").val(),  
  150.                     color: $("#mapColor").val(),  
  151.                     displayNegative: true,  
  152.                     sizeBy: 'width',  
  153.                     cursor: 'pointer',  
  154.                     negativeColor: Highcharts.getOptions().colors[0]  
  155.                 }  
  156.                 ],  
  157.                 chart: {  
  158.                     marginRight: 50  
  159.                 },  
  160.                 exporting: {  
  161.                     enabled: false  
  162.                 }  
  163.             }, function (chart) {  
  164.                 updateZoom(chart);  
  165.             });  
  166.         }  
  167.   
  168.     </script>  
  169. </head>  
  170. <body>  
  171.     Bubble Map Demo - SibeeshPassion  
  172.     <br />  
  173.     <br />  
  174.     <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>  
  175.     <label>Min Bubble Size</label>  
  176.     <input type="number" value="8" min="1" id="mapMinBubSize" title="Type Bubble Min Size." />  
  177.     <label>Max Bubble Size</label>  
  178.     <input type="number" min="1" value="40" id="mapMaxBubSize" title="Type Bubble Max Size." />  
  179.     <label>Color</label>  
  180.     <input type="color" value="#BAD6F2" id="mapColor" title="Select Bubble Color." />  
  181.     <label>Select Map</label>  
  182.     <select name="tech" id="mapSelectMap" class="tech" title="Select Map.">  
  183.         <option value="0">Select Map</option>  
  184.         <option value="countries/us/us-all" selected="selected">US alone</option>  
  185.         <option value="custom/world">Custom World</option>  
  186.     </select>  
  187.     <div id="apply" style="cursor: pointer; background-color: red; color: white; width: 100px; padding: 5px; float: right;">Apply Settings</div>  
  188. </body>  
  189.   
  190. </html>  

Conclusion

Please download the source code for more details. I hope you liked this article. Now please share your thoughts and suggestions. It matters a lot.

Up Next
    Ebook Download
    View all
    Learn
    View all