Wednesday, July 15, 2009

3D Model positioning in Google Earth API

I've been creating a demo using the Google Earth API.

I was trying to position a 3D model 'myModel.dae' (a Collada file exported from Google Skechup or downloaded from 3D Warehouse) at a fixed height above the ground, say 300 meters.

Following the code in the Google Earth API Sample Gallery, I attempted to use the kmlModel object's setAltitude method (and also setLatLngAlt).


function PlaceModel() {
var placemark = ge.createPlacemark('');
placemark.setName('model');
myModel = ge.createModel('');
ge.getFeatures().appendChild(placemark);
var loc = ge.createLocation('');

var link = ge.createLink('');

// A textured model created in Sketchup and exported as Collada.
link.setHref('myModel.dae');
myModel.setLink(link);

loc.setLatLngAlt(initLat, initLon, initAlt);

placemark.setGeometry(myModel);
}


This never worked. I tried numerous permutations of setting the altitude to no avail.

I eventually tried adding the following:
...
myModel = ge.createModel('');
myModel.setAltitudeMode(ge.ALTITUDE_ABSOLUTE); //<-- Without this, model is 'clamped to ground'
ge.getFeatures().appendChild(placemark);
...

You need to add the ge.ALTITUDE_ABSOLUTE to the .setAltitudeMode() to get it to be not clamped to ground.

Cheers!

No comments:

Post a Comment