rectangle-overlay-params.html
author sl
Wed, 09 Apr 2014 13:41:29 +0200
changeset 1 a1483545da35
parent 0 5ecce0e1ef52
child 2 b014e6b2a9e2
permissions -rw-r--r--
Rectangle overlay params:
Fixing duplicate first rectangle entry due to our guery strings parsing.
Adding 'a' URL parameter to control rectangle fill opacity.
Adding support to pass in color as hexa using %23 URL encoding.
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@0
    58
		
sl@0
    59
		//Display help if expected parameters are not specified
sl@0
    60
		if ( typeof qs.s === "undefined" || typeof qs.w === "undefined" || typeof qs.n === "undefined" || typeof qs.e === "undefined") {
sl@0
    61
				document.getElementById('help').innerHTML='Pass the URI parameters as follow: \
sl@0
    62
					<ul>\
sl@0
    63
					<li>\'s\' for southernmost latitude.</li>\
sl@0
    64
					<li>\'n\' for northernmost latitude.</li>\
sl@0
    65
					<li>\'e\' for easternmost longitude.</li>\
sl@0
    66
					<li>\'w\' for westernmost longitude.</li>\
sl@1
    67
					<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
    68
					<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@0
    69
					<li>\'width\' Map width in pixels, default is 1200.</li>\
sl@0
    70
					<li>\'height\' Map heigh in pixels, default is 800.</li>\
sl@0
    71
					</ul>\
sl@0
    72
					You can specify any number of rectangles.<br />\
sl@0
    73
					Map will be centered on the first rectangle.<br />\
sl@1
    74
					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
    75
sl@0
    76
			//Still display the map
sl@0
    77
			map = new google.maps.Map(document.getElementById('map'), {
sl@0
    78
					'zoom': 3,
sl@0
    79
					'center': new google.maps.LatLng(45,0),
sl@0
    80
					'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
    81
					});
sl@0
    82
			
sl@0
    83
			return;
sl@0
    84
		}
sl@0
    85
		
sl@0
    86
sl@0
    87
		
sl@0
    88
        var latLngBounds = new google.maps.LatLngBounds(
sl@0
    89
          new google.maps.LatLng(qs.s[0], qs.w[0]),
sl@0
    90
          new google.maps.LatLng(qs.n[0], qs.e[0])
sl@0
    91
        );
sl@0
    92
	  
sl@0
    93
        map = new google.maps.Map(document.getElementById('map'), {
sl@0
    94
          'zoom': 12,
sl@0
    95
          'center': new google.maps.LatLng(latLngBounds.getCenter().lat(),latLngBounds.getCenter().lng()),
sl@0
    96
          'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
    97
        });
sl@0
    98
sl@1
    99
		//window.alert(qs.s.length);
sl@0
   100
        
sl@0
   101
		//Create all our rectangles
sl@1
   102
		rectangles = []; //Create rectangles array
sl@0
   103
		for (var i = 0; i < qs.s.length; i++) {
sl@1
   104
		rectangles.push(new google.maps.Rectangle({	map: map,
sl@1
   105
													fillColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@1
   106
													fillOpacity: (typeof qs.a === "undefined" || typeof qs.a[i] === "undefined"?'0.2':decodeURIComponent(qs.a[i])),
sl@1
   107
													strokeColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':decodeURIComponent(qs.c[i])),
sl@1
   108
													strokeOpacity: 1.0,
sl@1
   109
													strokeWeight: 1
sl@1
   110
													}));
sl@1
   111
		//window.alert(decodeURIComponent(qs.c[i]));
sl@0
   112
		}
sl@0
   113
		
sl@0
   114
        redraw();
sl@0
   115
      }
sl@0
   116
      
sl@0
   117
      /**
sl@0
   118
       * Updates the Rectangle's bounds to resize its dimensions.
sl@0
   119
       */
sl@1
   120
      function redraw() {	  
sl@0
   121
	    var qs = GetQueryString();
sl@0
   122
		for (var i = 0; i < qs.s.length; i++) {		
sl@0
   123
        var latLngBounds = new google.maps.LatLngBounds(
sl@0
   124
          new google.maps.LatLng(qs.s[i], qs.w[i]),
sl@0
   125
          new google.maps.LatLng(qs.n[i], qs.e[i])
sl@0
   126
        );
sl@0
   127
        rectangles[i].setBounds(latLngBounds);
sl@0
   128
		}
sl@0
   129
      }
sl@0
   130
	  
sl@0
   131
  /** 
sl@0
   132
   * This function is anonymous, is executed immediately and 
sl@0
   133
   * the return value is assigned to QueryString!
sl@0
   134
   */ 	  
sl@0
   135
	 function GetQueryString() {
sl@0
   136
	  var query_string = {};
sl@0
   137
	  var query = window.location.search.substring(1);
sl@0
   138
	  var vars = query.split("&");
sl@1
   139
	  //window.alert("URL param count: " + vars.length);
sl@1
   140
	  for (var i=0;i<vars.length;i++) {	  
sl@0
   141
		var pair = vars[i].split("=");
sl@0
   142
			// If first entry with this name
sl@0
   143
		if ((typeof query_string[pair[0]] === "undefined") ) {
sl@1
   144
		  //Instantiate an array for that parameter
sl@1
   145
 		  var arr = [ pair[1] ];
sl@0
   146
		  query_string[pair[0]] = arr;
sl@1
   147
		  //window.alert("Create param '" + pair[0] + "' with value: " + pair[1]);
sl@0
   148
		} else {
sl@1
   149
		  //Add a value to our existing parameter array
sl@1
   150
		  //window.alert("Add param '" + pair[0] + "' with value: " + pair[1]);
sl@0
   151
		  query_string[pair[0]].push(pair[1]);
sl@0
   152
		}
sl@0
   153
	  } 
sl@0
   154
		return query_string;
sl@0
   155
	}
sl@0
   156
sl@0
   157
      // Register an event listener to fire when the page finishes loading.
sl@0
   158
      google.maps.event.addDomListener(window, 'load', init);
sl@0
   159
    </script>
sl@0
   160
  </head>
sl@0
   161
  <body>
sl@0
   162
    <h2>Google maps rectangle overlay</h2>
sl@0
   163
	<div id="help"><a href='rectangle-overlay-params.html'>help</a></div>
sl@0
   164
    <div id="map"></div>
sl@0
   165
  </body>
sl@0
   166
</html>
sl@0
   167