Click here to Skip to main content
12,702,596 members (32,995 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

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.


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 -->
<meta charset="utf-8">
<title>Finding Treasure on Google Map[</title>
html,body {
    height: 100%;
    margin: 0;
    padding: 0;

#map-holder {
    height: 100%;


// 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);

  //  5 markers to the map at random locations
  var lngSpan = northEast.lng() - southWest.lng();
  var latSpan = -;
  for (var i = 0; i < 5; i++) { 
    var position = new google.maps.LatLng( + 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() {'map'), marker);
    if (num == 0) {
        } else {

// call initialize() function upon completion of window load
google.maps.event.addDomListener(window, 'load', initialize);
    <div id="map-holder"></div>

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.



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


About the Author

Peter Leow
Instructor / Trainer
Singapore Singapore
Stop by my website at

Check out my publications at

Follow me on twitter at

“Live as if you were to die tomorrow. Learn as if you were to live forever.”
― Mahatma Gandhi


You may also be interested in...

Comments and Discussions

GeneralFinding Treasure on Google Map Pin
Member 1074494723-Jun-14 23:52
memberMember 1074494723-Jun-14 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.170118.1 | Last Updated 26 Mar 2014
Article Copyright 2014 by Peter Leow
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid