Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Finding Treasure on Google Map

4.71/5 (6 votes)
26 Mar 2014CPOL1 min read 18.9K   208  
A brief encounter with computer programming on Google map.
Image 1

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.

HTML
<!DOCTYPE html>
<!-- Author: Peter Leow @ 2014 -->
<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>

// change to the latitude and longitude of the centre of any area
// default is Singapore
var area = new google.maps.LatLng(1.3520830, 103.819836);

// prepare the images
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 to be called upon window load event
function initialize() {
    
    // define the map and its options
    var mapOptions = {
        zoom: 2,
        center: area,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // instantiate a Map object
    var map = new google.maps.Map(document.getElementById('map-holder'), mapOptions);

    // define the bound of the map
    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);

  //  5 markers to the map at random locations
  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('');
        }
  });
}

// call initialize() function upon completion of window load
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)