Introduction
One day, I was asked to conduct a one-hour+ introductory lesson on computer programming to a group of young people aged 14+. I pondered what I could achieve in such a short duration. My idea was to give them some quick hands-on after which they could see the fruit of their work right away and be amazed (at least for a while). So, out came this idea of Finding Treasure on Google Map.
Using the Code
This is a one-page HTML file incorporating Google Maps JavaScript API v3. Using JavaScript as the programming language, it has covered fundamental programming concepts such as variables, loop, condition, and function, albeit a very brief one. The code has been commented as much as possible to facilitate learning.
<!DOCTYPE html>
<!--
<html>
<head>
<meta charset="utf-8">
<title>Finding Treasure on Google Map[</title>
<style>
html,body {
height: 100%;
margin: 0;
padding: 0;
}
#map-holder {
height: 100%;
}
</style>
<script
src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var area = new google.maps.LatLng(1.3520830, 103.819836);
var fullChestImage = {
url: 'full_chest.png',
scaledSize: new google.maps.Size(50, 50)
}
var emptyChestImage = {
url: 'empty_chest.png',
scaledSize: new google.maps.Size(50, 50)
}
function initialize() {
var mapOptions = {
zoom: 2,
center: area,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-holder'), mapOptions);
var southWest = new google.maps.LatLng(1.2800, 103.69034);
var northEast = new google.maps.LatLng(1.45572, 103.94165);
var bounds = new google.maps.LatLngBounds(southWest, northEast);
map.fitBounds(bounds);
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i = 0; i < 5; i++) {
var position = new google.maps.LatLng(
southWest.lat() + latSpan * Math.random(),
southWest.lng() + lngSpan * Math.random());
var marker = new google.maps.Marker({
position: position,
title: 'Are you sure?',
map: map,
animation: google.maps.Animation.DROP
});
attachSecret(marker, i);
}
}
function attachSecret(marker, num) {
var message = ['Congratulation!', 'Nope', 'Nope', 'Nope', 'Nope'];
var infowindow = new google.maps.InfoWindow({
content: message[num],
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(marker.get('map'), marker);
if (num == 0) {
marker.setIcon(fullChestImage);
marker.setTitle('Congratulation!');
} else {
marker.setIcon(emptyChestImage);
marker.setTitle('');
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-holder"></div>
</body>
</html>
Points of Interest
This little program has served its purpose well. There is no special software needed, just some simple text editor for entering the code. The scope is also just right considering the short duration and the tender age of the audiences. Most importantly, after this brief encounter with computer programming, the young people came to realize that beneath all those fanciful animations and activities on a computer screen is a bunch of programming codes that drive them.
We have to accept the fact that computer programming is not everyone's cup of tea, but hopefully it is yours. Happy coding.
Reference