How to achieve continuous rotation of Google earth?

5,958 views
Skip to first unread message

Razz

unread,
Jan 27, 2009, 5:04:41 PM1/27/09
to KML Developer Support - Google Earth Browser Plugin
Hi

I would like to have the Google Earth rotate continuously, I saw some
snippet of code
at http://earth-api-samples.googlecode.com/svn/trunk/demos/interactive/index.html
to rotate (or fly)

var oldFlyToSpeed = ge.getOptions().getFlyToSpeed();
ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);

function go(count) {
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
lookAt.setLongitude(lookAt.getLongitude() + 5);
ge.getView().setAbstractView(lookAt);

if (count < 40) {
setTimeout(function(){
go(count + 1);
}, 50);
} else {
ge.getOptions().setFlyToSpeed(oldFlyToSpeed);
}
}

go(0);



Is there a way to achieve continuous rotation

regards
Raj

JamesS

unread,
Jan 27, 2009, 5:52:28 PM1/27/09
to KML Developer Support - Google Earth Browser Plugin
Hi Raj,

You can trigger your camera move function on a frameend event to get
continuous motion.

Something like this:


function initCB(object){
ge = object;
ge.getWindow().setVisibility(true);
ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);

google.earth.addEventListener(ge, "frameend", tick);
}

var speed = 10; // degrees per second
var lastMillis = (new Date()).getTime();


function tick(){

var now = (new Date()).getTime();
// dt is the delta-time since last tick, in seconds
var dt = (now - lastMillis) / 1000.0;
lastMillis = now;

var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
lookAt.setLongitude(lookAt.getLongitude() + speed*dt);

lookAt.setHeading(0); // Workaround for heading bug, issue #148
ge.getView().setAbstractView(lookAt);

}


------


Hope that Helps

- James

On Jan 27, 10:04 pm, Razz wrote:
> Hi
>
> I would like to have the Google Earth rotate continuously,  I saw some
> snippet of code
> athttp://earth-api-samples.googlecode.com/svn/trunk/demos/interactive/i...

shsavage

unread,
Jan 28, 2009, 9:10:50 AM1/28/09
to KML Developer Support - Google Earth Browser Plugin
Hi Raj,

You can acheive the same effect without the timer stuff like this
(change the rotation speed by changing setFlyToSpeed):

function initCallback(object) {
ge = object;
ge.getWindow().setVisibility(true);
ge.getOptions().setFlyToSpeed(4);
google.earth.addEventListener(ge, "frameend", rotateEarth);
}

function rotateEarth(){
google.earth.addEventListener(ge, "frameend", rotateEarth);
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
var myLon = lookAt.getLongitude();
if (myLon<350) { myLon = myLon + 10; } else { myLon=0; }
lookAt.setLongitude(myLon);
lookAt.setHeading(0); // Workaround for heading bug, issue
#148
ge.getView().setAbstractView(lookAt);
}

-Steve
> > Raj- Hide quoted text -
>
> - Show quoted text -

cjorba

unread,
Jan 29, 2009, 4:54:39 AM1/29/09
to KML Developer Support - Google Earth Browser Plugin
This topic was previosuly been discussed on:
http://groups.google.com/group/google-earth-browser-plugin/browse_thread/thread/6609a29a0b07b35c/3acbedfbf58c1e2b?hl=en#3acbedfbf58c1e2b

shsavage's line: lookAt.setHeading(0); // Workaround for heading
bug, issue #148
it's certainly a needed workaround if you don't want the globe
rotating horizontally suddenly.

-Carlos

shsavage

unread,
Jan 29, 2009, 1:38:00 PM1/29/09
to KML Developer Support - Google Earth Browser Plugin
To give credit where it's due, the workaround line came from JamesS
example. It is an interesting effect without it!
I also noticed that I had added the listener in two places, as I was
setting this up to have buttons to turn the rotation on and off. If
you want the rotation always on, you can remove the
"google.earth.addEventListener(ge, "frameend", rotateEarth); " line in
the rotateEarth function.

-Steve

On Jan 29, 2:54 am, cjorba wrote:
> This topic was previosuly been discussed on:http://groups.google.com/group/google-earth-browser-plugin/browse_thr...
> > > - Show quoted text -- Hide quoted text -

Razz

unread,
Jan 30, 2009, 12:13:44 PM1/30/09
to KML Developer Support - Google Earth Browser Plugin
Thanx for the suggestions.

I want to stop and start rotation on a keyPress event. I tried to use
the code from the above post, but my earth moves only a few notch
before it stops. I think I am doing something silly

any advise on what am I missing. I don't think I can put a recursive
loop, because this might cause the plugin thread to go out of control?

<div id="rotation"><a href="javascript:toggleRotation()">Rotate</a></
div>
i.e.
function toggleRotation() {
if(boolRotate) {
boolRotate = false;
rotateEarth();
} else {
stopRotation();
boolRotate = true;
}
}

function rotateEarth(){
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
var myLon = lookAt.getLongitude();
if (myLon<350) { myLon = myLon + 10; } else { myLon=0; }
lookAt.setLongitude(myLon);
lookAt.setHeading(0); // Workaround for heading bug, issue #148
lookAt.setAltitude(10000000);
lookAt.setRange(1000);
ge.getView().setAbstractView(lookAt);
}


Razz

unread,
Jan 30, 2009, 12:53:04 PM1/30/09
to KML Developer Support - Google Earth Browser Plugin
This seems to work, but I don't know whether it is the right way
<div id="rotation"><a href="javascript:toggleRotation
()">Rotate</a></div>

function toggleRotation() {
if(boolRotate) {
boolRotate = false;
google.earth.addEventListener(ge, "frameend", rotateEarth);
} else {
google.earth.removeEventListener(ge, "frameend", rotateEarth);
boolRotate = true;
}
}


Every time I toggle using the javascript, this seems to work....any
thoughts on whether it is correct approach?

Roman N

unread,
Jan 30, 2009, 2:57:36 PM1/30/09
to KML Developer Support - Google Earth Browser Plugin
Looks good to me!

- Roman

cjorba

unread,
Feb 1, 2009, 9:24:51 AM2/1/09
to KML Developer Support - Google Earth Browser Plugin
keep in mind that you need a frame to be painted so that the
"frameend" event is triggered, so to make sure that the globe starts
rotating every time you press the toggle button the code should be
something like this:

function toggleRotation() {
if(boolRotate) {
boolRotate = false;
google.earth.addEventListener(ge,
"frameend", rotateEarth);
rotateEarth(); //needed to trigger
the first frameend event
} else {
google.earth.removeEventListener(ge,
"frameend", rotateEarth);
boolRotate = true;
}
}
If you don't do this initial earth movement, then it won't start
rotating until you manually move something in the GE screen.

shsavage

unread,
Feb 5, 2009, 2:15:42 PM2/5/09
to KML Developer Support - Google Earth Browser Plugin
Hi Razz,

I've got start and stop buttons implemented on a test platform, at
http://gaialab.asu.edu/Test. cjorba is correct, you have to trigger
the rotation when you add the frameend listener. IO did it with the
following code and HTML:

<script>
google.load("earth", "1");

var ge = null;
var speed = 15; // degrees per second
var lastMillis = (new Date()).getTime();

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

function failureCallback(object) {
}

function initCallback(object) {
ge = object;
ge.getWindow().setVisibility(true);
ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);
google.earth.addEventListener(ge, "frameend", rotateEarth);
}

function rotateEarth(){
var now = (new Date()).getTime();
// dt is the delta-time since last tick, in seconds
var dt = (now - lastMillis) / 1000.0;
lastMillis = now;
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
lookAt.setLongitude(lookAt.getLongitude() + speed*dt);
lookAt.setHeading(0); // Workaround for heading bug, issue
#148
ge.getView().setAbstractView(lookAt);
}

function startRotation() {
google.earth.addEventListener(ge, "frameend", rotateEarth);
rotateEarth();
}

function stopRotation() {
google.earth.removeEventListener(ge, "frameend",
rotateEarth);
}

function toggleButtons(val) {
frm=document.forms[0]
if(val=="start")
{frm.start.disabled=true;frm.stop.disabled=false;}
if(val=="stop")
{frm.start.disabled=false;frm.stop.disabled=true;}
}
</script>
</head>
<body onload='init()' id='body'>
<center>
<div id='map3d_container' style='border: thin solid blue;
height: 600px; width: 800px;'>
<div id='map3d' style='height: 100%;'></div>
</div>
<form>
<button id="start" onclick="startRotation(); toggleButtons
('start')">Start Rotation</button>&nbsp;&nbsp;&nbsp;
<button id="stop" onclick="stopRotation(); toggleButtons
('stop')">Stop Rotation</button>
</form>
</center>
</body>
</html>

-Steve
Reply all
Reply to author
Forward
0 new messages