CS2020:   Web Science, Sytems and Design

Google Maps: Hello World Simple Google Maps JavaScript

Try it (sensor set to true)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
  }

</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

Code Explained

  1. We declare the application as HTML5 using the <!DOCTYPE html> declaration.
  2. We include the Maps API JavaScript using a script tag.
  3. We create a div element named "map_canvas" to hold the Map.
  4. We create a JavaScript object literal to hold a number of map properties.

    We also set the initial zoom level and mapTypeId to google.maps.MapTypeId.ROADMAP. The following types are supported:

    • ROADMAP displays the normal, default 2D tiles of Google Maps.
    • SATELLITE displays photographic tiles.
    • HYBRID displays a mix of photographic tiles and a tile layer for prominent features (roads, city names).
    • TERRAIN displays physical relief tiles for displaying elevation and water features (mountains, rivers, etc.).


  5. We write a JavaScript function to create a "map" object.
  6. We initialize the map object from the body tag's onload event.

Note: Zoom = 0 Earth Level
          Zoom = higher number More zoomed in

© Lynne Grewe