Search code examples
javascriptinternet-explorergoogle-earth

Loading/Refreshing 3d model in Google Earth


I'm using Google Earth in Web-development but I'm facing a weird situation, this is a simple code from the google code playground and it loads a 3D model into a google earth, if I refresh the page it wont load the model again, http://savedbythegoog.appspot.com/?id=7f8638b214605a2327af223c613a6ae13874416b

Is there any way to fix it.

I'm facing with one more problem of loading the 3D models in Internet Explorer 8 in 32 bit XP machine, IE8 doesn't load the 3d model in google earth, you can check the link given. I'm also posting the js code below.

Please Help

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Earth API Sample</title>
<script src="http://www.google.com/jsapi?key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script>
<script type="text/javascript">
  function addSampleButton(caption, clickHandler) {
    var btn = document.createElement('input');
    btn.type = 'button';
    btn.value = caption;

    if (btn.attachEvent)
      btn.attachEvent('onclick', clickHandler);
    else
      btn.addEventListener('click', clickHandler, false);

    // add the button to the Sample UI
    document.getElementById('sample-ui').appendChild(btn);
  }

  function addSampleUIHtml(html) {
    document.getElementById('sample-ui').innerHTML += html;
  }
</script>
<script type="text/javascript">
var ge;

google.load("earth", "1");

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

 // addSampleButton('Create a 3D Model!', buttonClick);
}

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

  // add a navigation control
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

  // add some layers
  ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

  var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
  la.setRange(100000);
  ge.getView().setAbstractView(la);

  create3dModel();

  document.getElementById('installed-plugin-version').innerHTML =
    ge.getPluginVersion().toString();
}

function failureCallback(errorCode) {
}

function create3dModel() {
  // Create a 3D model, initialize it from a Collada file, and place it
  // in the world.

  var placemark = ge.createPlacemark('');
  placemark.setName('model');
  var model = ge.createModel('');
  ge.getFeatures().appendChild(placemark);
  var loc = ge.createLocation('');
  model.setLocation(loc);
  var link = ge.createLink('');

  // A textured model created in Sketchup and exported as Collada.
  link.setHref('http://earth-api-samples.googlecode.com/svn/trunk/examples/' +
               'static/splotchy_box.dae');
  model.setLink(link);

  var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
  loc.setLatitude(la.getLatitude());
  loc.setLongitude(la.getLongitude());

  placemark.setGeometry(model);

  la.setRange(300);
  la.setTilt(45);
  ge.getView().setAbstractView(la);
}

function buttonClick() {
  create3dModel();
}

</script>
  </head>
  <body onload="init()" style="font-family: arial, sans-serif; font-size: 13px; border: 0;">
  <div id="sample-ui"></div>
  <div id="map3d" style="width: 500px; height: 380px;"></div>
  <br>
  <div>Installed Plugin Version: <span id="installed-plugin-version" style="font-weight: bold;">Loading...</span></div>
 </body>
</html>

If that links doesn't work copy the code and paste it in a text file and rename it to .html and then execute it.


Solution

  • Even I had faced the same problem with collada models and internet explorer, the code u have given is working fine in rest of the browsers like chrome and mozilla.

    check out this link, but in this case they are loading a kml file and not directly loading collada models into the browser using javascript.