Week 2 Solution

<!DOCTYPE html>
<html>
	<head>
		<!-- This is the HEAD of the HTML file, things in here do not appear on the page. It is 
		 used for settings, such as the language and page title, as well as loading CSS styles and 
		 scripts that you want to load before the content of the page. --> 
		<meta charset="UTF-8">
		<title>GIS and the Web</title>
		
		<!-- This is loading the stylesheet for the Leaflet map from their website. 
		 Make sure you put this BEFORE Leaflet's JavaScript -->
		<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"/>

		<!-- This is loading the Leaflet JavaScript library from their website
		 Make sure you put this AFTER Leaflet's CSS -->
		<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"></script>
		 
		<!-- Load the Proj4js library -->
		<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>

		<!-- This is where we set the style for the different elements in the page's content -->
		<style>
		
			/* Set the font for the entire page */
			html, body {
				padding: 0;
				margin: 0;
				width: 100%;
				height: 100%;
			}
		
			/* Style the map */
			#map {
				/* This is the dimensions of the map on the screen, you can set it in 
				 px (pixels) or % of the total screen size*/
				width:  100%;
				height: 100%;
			}
			
		</style>
	</head>
	
	<!-- The onload event of the body tag is being used to call the function that creates the map.
	 This means that nothing will happen until the body is completely loaded, giving us a little more
	 control over the order in which the web page loads. -->
	<body onload='initMap();'> 
	
		<!-- This is where the map will go -->
		<div id='map'></div>
		
		<!-- This is where we put our JavaScript, note that it is below the div, as the div needs
		 to exist before we can put a map in it!-->
		<script>		
			
			//setup global variables
			var map, transformer, marker;
			
			/**
			 * Initialise the Map
			 */
			function initMap(){
			
				// this is a variable that holds the reference to the Leaflet map object
				map = L.map('map').setView([55, -4], 6);

				// this adds the basemap tiles to the map
				L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
					attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
				}).addTo(map);
			
				// set the proj4 string for the WGS84 CRS
				// http://spatialreference.org/ref/epsg/wgs-84/proj4/
				var P_wgs84 = "+proj=longlat +datum=WGS84 +no_defs";
				
				// set the proj4 string for the OSGB CRS
				// http://spatialreference.org/ref/epsg/osgb-1936-british-national-grid/proj4/
				var P_osgb = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs";
				
				// build a proj4 object to handle forward and inverse transformations
				transformer = proj4(P_wgs84, P_osgb);
				 
				// add listener for click event on the map
				map.on('click', onMapClick);
			
			}
			
			/**
			 * Event handler for map click
			 */
			function onMapClick(e) {
			
				// if the marker already exists, remove it from the map
				if (marker){
					map.removeLayer(marker);
				}
			
				// transform coordinates to OSGB
				var xy = transformer.forward([e.latlng.lng, e.latlng.lat]);
			
				// add marker to map at the clicked location
				marker = L.marker(e.latlng).addTo(map);
				
				// open popup displaying OSGB coordinates
				marker.bindPopup(parseInt(xy[0]).toString() + ", " + parseInt(xy[1]).toString()).openPopup();
			}

		</script>
	</body>
</html>