rectangle-overlay-params.html
author sl
Wed, 09 Apr 2014 12:36:39 +0200
changeset 0 5ecce0e1ef52
child 1 a1483545da35
permissions -rw-r--r--
Various gmaps HTML pages and scripts.
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@0
    42
	  	var qs = GetQueryString();
sl@0
    43
		
sl@0
    44
		//Set map width if specified
sl@0
    45
		if (!(typeof qs.width === "undefined"))	{
sl@0
    46
			document.getElementById('map').style.width=qs.width[0].concat("px");
sl@0
    47
			//window.alert("width");
sl@0
    48
		}
sl@0
    49
		
sl@0
    50
		//Set map height if specified
sl@0
    51
		if (!(typeof qs.height === "undefined"))	{
sl@0
    52
			document.getElementById('map').style.height=qs.height[0].concat("px");	
sl@0
    53
			//window.alert("height");
sl@0
    54
		}
sl@0
    55
		
sl@0
    56
		//Display help if expected parameters are not specified
sl@0
    57
		if ( typeof qs.s === "undefined" || typeof qs.w === "undefined" || typeof qs.n === "undefined" || typeof qs.e === "undefined") {
sl@0
    58
				document.getElementById('help').innerHTML='Pass the URI parameters as follow: \
sl@0
    59
					<ul>\
sl@0
    60
					<li>\'s\' for southernmost latitude.</li>\
sl@0
    61
					<li>\'n\' for northernmost latitude.</li>\
sl@0
    62
					<li>\'e\' for easternmost longitude.</li>\
sl@0
    63
					<li>\'w\' for westernmost longitude.</li>\
sl@0
    64
					<li>\'c\' Specify a <a href="http://www.w3schools.com/cssref/css_colornames.asp">color name</a> for your rectangles.</li>\
sl@0
    65
					<li>\'width\' Map width in pixels, default is 1200.</li>\
sl@0
    66
					<li>\'height\' Map heigh in pixels, default is 800.</li>\
sl@0
    67
					</ul>\
sl@0
    68
					You can specify any number of rectangles.<br />\
sl@0
    69
					Map will be centered on the first rectangle.<br />\
sl@0
    70
					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=red&c=green">here</a> for an example.';
sl@0
    71
sl@0
    72
			//Still display the map
sl@0
    73
			map = new google.maps.Map(document.getElementById('map'), {
sl@0
    74
					'zoom': 3,
sl@0
    75
					'center': new google.maps.LatLng(45,0),
sl@0
    76
					'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
    77
					});
sl@0
    78
			
sl@0
    79
			return;
sl@0
    80
		}
sl@0
    81
		
sl@0
    82
sl@0
    83
		
sl@0
    84
        var latLngBounds = new google.maps.LatLngBounds(
sl@0
    85
          new google.maps.LatLng(qs.s[0], qs.w[0]),
sl@0
    86
          new google.maps.LatLng(qs.n[0], qs.e[0])
sl@0
    87
        );
sl@0
    88
	  
sl@0
    89
        map = new google.maps.Map(document.getElementById('map'), {
sl@0
    90
          'zoom': 12,
sl@0
    91
          'center': new google.maps.LatLng(latLngBounds.getCenter().lat(),latLngBounds.getCenter().lng()),
sl@0
    92
          'mapTypeId': google.maps.MapTypeId.ROADMAP
sl@0
    93
        });
sl@0
    94
sl@0
    95
        
sl@0
    96
		//Create all our rectangles
sl@0
    97
		rectangles = []; //Create rentangles array
sl@0
    98
		for (var i = 0; i < qs.s.length; i++) {
sl@0
    99
		rectangles.push(new google.maps.Rectangle({map: map,fillColor: (typeof qs.c === "undefined" || typeof qs.c[i] === "undefined"?'#666666':qs.c[i]) }));
sl@0
   100
		//window.alert(qs.c[i]);
sl@0
   101
		}
sl@0
   102
		
sl@0
   103
        redraw();
sl@0
   104
      }
sl@0
   105
      
sl@0
   106
      /**
sl@0
   107
       * Updates the Rectangle's bounds to resize its dimensions.
sl@0
   108
       */
sl@0
   109
      function redraw() {
sl@0
   110
	    var qs = GetQueryString();
sl@0
   111
		for (var i = 0; i < qs.s.length; i++) {		
sl@0
   112
        var latLngBounds = new google.maps.LatLngBounds(
sl@0
   113
          new google.maps.LatLng(qs.s[i], qs.w[i]),
sl@0
   114
          new google.maps.LatLng(qs.n[i], qs.e[i])
sl@0
   115
        );
sl@0
   116
        rectangles[i].setBounds(latLngBounds);
sl@0
   117
		}
sl@0
   118
      }
sl@0
   119
	  
sl@0
   120
  /** 
sl@0
   121
   * This function is anonymous, is executed immediately and 
sl@0
   122
   * the return value is assigned to QueryString!
sl@0
   123
   */ 	  
sl@0
   124
	 function GetQueryString() {
sl@0
   125
	  var query_string = {};
sl@0
   126
	  var query = window.location.search.substring(1);
sl@0
   127
	  var vars = query.split("&");
sl@0
   128
	  for (var i=0;i<vars.length;i++) {
sl@0
   129
		var pair = vars[i].split("=");
sl@0
   130
			// If first entry with this name
sl@0
   131
		if ((typeof query_string[pair[0]] === "undefined") ) {
sl@0
   132
		  query_string[pair[0]] = pair[1];
sl@0
   133
 		  var arr = [ query_string[pair[0]], pair[1] ];
sl@0
   134
		  query_string[pair[0]] = arr;
sl@0
   135
		} else {
sl@0
   136
		  query_string[pair[0]].push(pair[1]);
sl@0
   137
		}
sl@0
   138
	  } 
sl@0
   139
		return query_string;
sl@0
   140
	}
sl@0
   141
sl@0
   142
      // Register an event listener to fire when the page finishes loading.
sl@0
   143
      google.maps.event.addDomListener(window, 'load', init);
sl@0
   144
    </script>
sl@0
   145
  </head>
sl@0
   146
  <body>
sl@0
   147
    <h2>Google maps rectangle overlay</h2>
sl@0
   148
	<div id="help"><a href='rectangle-overlay-params.html'>help</a></div>
sl@0
   149
    <div id="map"></div>
sl@0
   150
  </body>
sl@0
   151
</html>
sl@0
   152