rectangle-overlay-params.html
author sl
Thu, 10 Apr 2014 12:45:33 +0200
changeset 3 54ac29018aaf
parent 2 b014e6b2a9e2
permissions -rw-r--r--
Rectangle overlay params:
Adding support for 'cycle' and 'zoom' param.
Fixing issue with step not being interpreted and an integer.
Updating help documentation.
sl@0
     1
<!DOCTYPE html>
sl@0
     2
<html>
sl@0
     3
  <head>
sl@0
     4
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
sl@0
     5
    <title>Rectangle Overlay</title>
sl@0
     6
sl@0
     7
    <style type="text/css">
sl@0
     8
      #map {
sl@0
     9
        width: 1200px;
sl@0
    10
        height: 800px;
sl@0
    11
		margin: 5px;
sl@0
    12
      }
sl@0
    13
	  
sl@0
    14
	  h2 {
sl@0
    15
		margin: 2px;
sl@0
    16
	  }
sl@0
    17
	  
sl@0
    18
	  ul {
sl@0
    19
		margin: 2px;
sl@0
    20
	  }
sl@0
    21
	  
sl@0
    22
	  body {
sl@0
    23
		font-size: 12pt;
sl@0
    24
		font-family: Arial, sans-serif;
sl@0
    25
		}
sl@0
    26
    </style>
sl@0
    27
sl@0
    28
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
sl@0
    29
sl@0
    30
    <script type="text/javascript">
sl@0
    31
      // Global variables
sl@0
    32
      var map;
sl@0
    33
      var marker1;
sl@0
    34
      var marker2;
sl@0
    35
      var rectangle;
sl@0
    36
sl@0
    37
      /**
sl@0
    38
       * Called on the initial page load.
sl@0
    39
       */
sl@0
    40
      function init() {
sl@0
    41
	  
sl@1
    42
		//window.alert("Init called");
sl@1
    43
	  
sl@0
    44
	  	var qs = GetQueryString();
sl@1
    45
		//qs = decodeURIComponent(qs);
sl@0
    46
		
sl@0
    47
		//Set map width if specified
sl@0
    48
		if (!(typeof qs.width === "undefined"))	{
sl@0
    49
			document.getElementById('map').style.width=qs.width[0].concat("px");
sl@0
    50
			//window.alert("width");
sl@0
    51
		}
sl@0
    52
		
sl@0
    53
		//Set map height if specified
sl@0
    54
		if (!(typeof qs.height === "undefined"))	{
sl@0
    55
			document.getElementById('map').style.height=qs.height[0].concat("px");	
sl@0
    56
			//window.alert("height");
sl@0
    57
		}
sl@2
    58
sl@2
    59
		var animation=false;
sl@2
    60
		if (!(typeof qs.anim === "undefined"))	{
sl@3
    61
			animation=(qs.anim[0]=='true'||qs.anim[0]=='1');
sl@2
    62
			//window.alert("Using animation mode");
sl@2
    63
		}		
sl@2
    64
sl@2
    65
		var intervalInMs=3000; //default to n seconds
sl@2
    66
		if (!(typeof qs.interval === "undefined"))	{
sl@2
    67
			intervalInMs=qs.interval[0];
sl@2
    68
			//window.alert("Interval: " + intervalInMs + " ms");
sl@2
    69
		}
sl@3
    70
			
sl@3
    71
		var cycle=false; //Whether we should cycle through our animation
sl@3
    72
		if (!(typeof qs.cycle === "undefined"))	{
sl@3
    73
			cycle=(qs.cycle[0]=='true'||qs.cycle[0]=='1');
sl@3
    74
			//window.alert("cycle: " + cycle);
sl@3
    75
		}
sl@2
    76
sl@3
    77
		var zoom=12; //Specify default zoom
sl@3
    78
		if (!(typeof qs.zoom === "undefined"))	{
sl@3
    79
			zoom=parseInt(qs.zoom[0]);
sl@3
    80
			//window.alert("zoom: " + zoom);
sl@3
    81
		}
sl@3
    82
sl@3
    83
		
sl@2
    84
		var step=1; //default to n seconds
sl@2
    85
		if (!(typeof qs.step === "undefined"))	{
sl@3
    86
			//Use parseInt to force that variable to an integer rather than a string
sl@3
    87
			step=parseInt(qs.step[0]);
sl@2
    88
			//window.alert("Step: " + step);
sl@2
    89
		}
sl@0
    90
		
sl@0
    91
		//Display help if expected parameters are not specified
sl@0
    92
		if ( typeof qs.s === "undefined" || typeof qs.w === "undefined" || typeof qs.n === "undefined" || typeof qs.e === "undefined") {
sl@0
    93
				document.getElementById('help').innerHTML='Pass the URI parameters as follow: \
sl@3
    94
					\
sl@3
    95
					<h3>Rectangles parameters:</h3>\
sl@0
    96
					<ul>\
sl@0
    97
					<li>\'s\' for southernmost latitude.</li>\
sl@0
    98
					<li>\'n\' for northernmost latitude.</li>\
sl@0
    99
					<li>\'e\' for easternmost longitude.</li>\
sl@0
   100
					<li>\'w\' for westernmost longitude.</li>\
sl@1
   101
					<li>\'c\' Specify a <a href="http://www.w3schools.com/cssref/css_colornames.asp">color name</a> or value (use %23FFFFFF for white) for your rectangles.</li>\
sl@1
   102
					<li>\'a\' Specify alpha opacity of your rectangle fill, it should be between 1 and 0. For instance 0.5 makes it half transparent.</li>\
sl@3
   103
					</ul>\
sl@3
   104
					\
sl@3
   105
					You can specify any number of rectangles.<br />\
sl@3
   106
					Map will be centered on the first rectangle.<br />\
sl@3
   107
					\
sl@3
   108
					<h3>Global parameters:</h3>\
sl@3
   109
					<ul>\
sl@0
   110
					<li>\'width\' Map width in pixels, default is 1200.</li>\
sl@0
   111
					<li>\'height\' Map heigh in pixels, default is 800.</li>\
sl@3
   112
					<li>\'zoom\' Specify default zoom.</li>\
sl@3
   113
					<li>\'anim\' Specify whether or not to perform an animation on our list of rectangles. By default no animation is performed.</li>\
sl@3
   114
					<li>\'interval\' The interval between each animation step specified in milliseconds (ms).</li>\
sl@3
   115
					<li>\'cycle\' Specify whether our not our animation is to keep on going forever or simple run once. By default it runs just once.</li>\
sl@3
   116
					<li>\'step\' Specify how many rectangles do we display on every animation tick.</li>\
sl@0
   117
					</ul>\
sl@3
   118
					\
sl@1
   119
					Click <a href="rectangle-overlay-params.html?w=8.619000&n=50.134350&e=8.714110&s=50.091180&w=8.657227&n=50.119630&e=8.673706&s=50.108644&w=8.652547&n=50.109318&e=8.658400&s=50.105320&c=black&c=%23FF0000&c=green&a=0.3&a=0.75&c=0.5">here</a> for an example.';
sl@0
   120
sl@0
   121
			//Still display the map
sl@0
   122
			map = new google.maps.Map(document.getElementById('map'), {
sl@0
   123
					'zoom': 3,
sl@0
   124
					'center': new google.maps.LatLng(45,0),
sl@0
   125
					'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
   126
					});
sl@0
   127
			
sl@0
   128
			return;
sl@0
   129
		}
sl@0
   130
		
sl@2
   131
		//We are not displaying help
sl@2
   132
		//Init our map position and zoom
sl@2
   133
		var latLngBounds = new google.maps.LatLngBounds(
sl@0
   134
          new google.maps.LatLng(qs.s[0], qs.w[0]),
sl@0
   135
          new google.maps.LatLng(qs.n[0], qs.e[0])
sl@0
   136
        );
sl@0
   137
	  
sl@0
   138
        map = new google.maps.Map(document.getElementById('map'), {
sl@3
   139
          'zoom': zoom,
sl@0
   140
          'center': new google.maps.LatLng(latLngBounds.getCenter().lat(),latLngBounds.getCenter().lng()),
sl@0
   141
          'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
   142
        });
sl@2
   143
				
sl@2
   144
		if (!animation) {
sl@2
   145
			//We are not doing animation so just display all our rectangles
sl@2
   146
			//window.alert(qs.s.length);        
sl@2
   147
			//Create all our rectangles
sl@2
   148
			rectangles = []; //Create rectangles array
sl@2
   149
			for (var i = 0; i < qs.s.length; i++) {
sl@2
   150
			rectangles.push(new google.maps.Rectangle({	map: map,
sl@2
   151
														fillColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@2
   152
														fillOpacity: (typeof qs.a === "undefined" || typeof qs.a[i] === "undefined"?'0.2':decodeURIComponent(qs.a[i])),
sl@2
   153
														strokeColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@2
   154
														strokeOpacity: 1.0,
sl@2
   155
														strokeWeight: 1,
sl@2
   156
														bounds: new google.maps.LatLngBounds(
sl@2
   157
														new google.maps.LatLng(qs.s[i], qs.w[i]),
sl@2
   158
														new google.maps.LatLng(qs.n[i], qs.e[i]))
sl@2
   159
														}));
sl@2
   160
			//window.alert(decodeURIComponent(qs.c[i]));		
sl@2
   161
			}
sl@2
   162
		}
sl@2
   163
		else {
sl@3
   164
			var currentRect=0;
sl@3
   165
			rectangles = []; //Create rectangles array
sl@3
   166
			//We are doing an animation so just setup our timer with proper function
sl@3
   167
			var animTimer=setInterval(function(){
sl@3
   168
				//alert("Hello");
sl@3
   169
				if ((currentRect+step)<qs.s.length) {
sl@2
   170
					//We are still in range, display our rect
sl@3
   171
					if (cycle) {
sl@3
   172
						rectangles.forEach(function(entry) {entry.setMap(null);});
sl@3
   173
						//Clear our rectangle array before adding the new ones
sl@3
   174
						rectangles = [];
sl@3
   175
					}
sl@3
   176
					
sl@3
   177
					//window.alert("Current rect: " + currentRect );
sl@2
   178
					for (var i=currentRect; i<(currentRect+step); i++) {
sl@2
   179
						rectangles.push(new google.maps.Rectangle({	map: map,
sl@3
   180
																	fillColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@3
   181
																	fillOpacity: (typeof qs.a === "undefined" || typeof qs.a[i] === "undefined"?'0.2':decodeURIComponent(qs.a[i])),
sl@3
   182
																	strokeColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@3
   183
																	strokeOpacity: 1.0,
sl@3
   184
																	strokeWeight: 1,
sl@3
   185
																	bounds: new google.maps.LatLngBounds(
sl@3
   186
																	new google.maps.LatLng(qs.s[i], qs.w[i]),
sl@3
   187
																	new google.maps.LatLng(qs.n[i], qs.e[i]))
sl@3
   188
																	}));
sl@2
   189
					}
sl@3
   190
				}
sl@3
   191
				else {
sl@2
   192
					//Not in range, go back to 0
sl@2
   193
					currentRect=0;
sl@3
   194
					if (!cycle) {
sl@3
   195
						//Stop our timer if we are not in cycle mode
sl@3
   196
						window.clearInterval(animTimer);
sl@3
   197
					}
sl@2
   198
					return;
sl@3
   199
				}
sl@3
   200
				//Move on to next frame					
sl@3
   201
				currentRect=currentRect+step;
sl@3
   202
			},
sl@3
   203
			intervalInMs);		
sl@0
   204
		}
sl@0
   205
      }
sl@0
   206
      
sl@2
   207
sl@0
   208
	  
sl@0
   209
  /** 
sl@0
   210
   * This function is anonymous, is executed immediately and 
sl@0
   211
   * the return value is assigned to QueryString!
sl@0
   212
   */ 	  
sl@0
   213
	 function GetQueryString() {
sl@0
   214
	  var query_string = {};
sl@0
   215
	  var query = window.location.search.substring(1);
sl@0
   216
	  var vars = query.split("&");
sl@1
   217
	  //window.alert("URL param count: " + vars.length);
sl@1
   218
	  for (var i=0;i<vars.length;i++) {	  
sl@0
   219
		var pair = vars[i].split("=");
sl@0
   220
			// If first entry with this name
sl@0
   221
		if ((typeof query_string[pair[0]] === "undefined") ) {
sl@1
   222
		  //Instantiate an array for that parameter
sl@1
   223
 		  var arr = [ pair[1] ];
sl@0
   224
		  query_string[pair[0]] = arr;
sl@1
   225
		  //window.alert("Create param '" + pair[0] + "' with value: " + pair[1]);
sl@0
   226
		} else {
sl@1
   227
		  //Add a value to our existing parameter array
sl@1
   228
		  //window.alert("Add param '" + pair[0] + "' with value: " + pair[1]);
sl@0
   229
		  query_string[pair[0]].push(pair[1]);
sl@0
   230
		}
sl@0
   231
	  } 
sl@0
   232
		return query_string;
sl@0
   233
	}
sl@0
   234
sl@0
   235
      // Register an event listener to fire when the page finishes loading.
sl@0
   236
      google.maps.event.addDomListener(window, 'load', init);
sl@0
   237
    </script>
sl@0
   238
  </head>
sl@0
   239
  <body>
sl@0
   240
    <h2>Google maps rectangle overlay</h2>
sl@0
   241
	<div id="help"><a href='rectangle-overlay-params.html'>help</a></div>
sl@0
   242
    <div id="map"></div>
sl@0
   243
  </body>
sl@0
   244
</html>
sl@0
   245