Click here to Skip to main content
Click here to Skip to main content

Finding Treasure on Google Map

, 26 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A brief encounter with computer programming on Google map.

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>
<!-- 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)

Share

About the Author

Peter Leow
Instructor / Trainer
Singapore Singapore
Graduated from the National University of Singapore with the following qualifications:
1. Graduate Diploma in Systems Analysis (2002)
2. Master of Technology (Knowledge Engineering) (2013)
 
Currently, lecturing in the area of social media and web development at a technical institution.
 
Having hibernated for ages, finally woke up to serve the Community in Oct 2013.
Follow on   LinkedIn

Comments and Discussions

 
GeneralFinding Treasure on Google Map PinmemberMember 1074494723-Jun-14 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 26 Mar 2014
Article Copyright 2014 by Peter Leow
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid