Deprecated
Google has now stopped its Earth API and Maps API is a paid for API nowadays. Some capabilities have also been either removed or changed. Therefore, I had to remove all examples of VizMap applications I had created. I didn’t remove the entire topic as I thought some of the code may still be useful with some modifications.
javaScript: Flying around and asynchronicity in google Earth
This relates to Data Driven Mapping applications and the Javascript HowTo section
Flying around a marker and the complications of asynchronicity
Most of the things in Google Earth happen asynchronously. That means that handling events and time outs is a fairly signifcant task when working with Earth. The VizMap applications provide the capability of flying round a spot. Flying is halted by clicking on the marker. This means dealing with the ‘frameend’ event, to control the smooth animation to the next viewpoint, and ‘viewchangeend’ so as to wait for the display to settle down before plotting balloons. In addition, I found that setting event listeners with the method of a custom object as the handler didn’t work (I never found out why), and I also discovered that firefox cannot handle (without crashing) changing a Dom element while it is still dealing with an information balloon (IE and Chrome seemed very happy with it).
I had created a “Royal Family application” in the middle of flying around Buckingham Palace. The flying was to stop when the queen’s picture was clicked. This also needed to return the viewing range to the default height, and to bring up the info balloon for this spot.
Walkthrough
Flying around is initialized as follows. We need to change the viewing range, remove any balloons on the screen and handle the ‘framend’ event. frameend is fired at the end of the animated sequence that will be caused by mcpherFly(spot).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
mcpherSpot.prototype.initFlyAround = function() { var spot = this; // been asked to rotate around the spot if (!spot.flying) { var vm = spot.parent; var ve = vm.ve; var ge = ve.ge; spot.flying = true; ge.setBalloon(null); spot.lookAt.setRange(parseFloat(vm.control.flyrange)); ge.getOptions().setFlyToSpeed(parseFloat(vm.control.flyspeed)); spot.initialLookAtHeading = spot.lookAt.getHeading(); google.earth.addEventListener(ge, 'frameend', function () { mcpherFly(spot); }); vm.statusUpdate('..flying around '+spot.title,true); mcpherFly(spot); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function mcpherFly(spot){ // whats the point of this? // seems that i can't addevent listener of spot.fly directly... spot.fly(); } mcpherSpot.prototype.fly = function() { // changing the heading and re- do var spot = this; if (spot.flying){ var vm = spot.parent; var ve=vm.ve; var ge = ve.ge; var newHeading = spot.lookAt.getHeading() + parseFloat(vm.control.flyincrement); if (newHeading > 360) { newHeading -= 360; } spot.lookAt.setHeading(newHeading); ve.showLookAt(spot); } return (spot.flying); } |
1 2 |
google.earth.addEventListener(spot.placemark, 'click', function(e) { mcpherClicks(e,spot,false); } ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function mcpherClicks(e,spot,plot){ if(e.preventDefault)e.preventDefault(); if(e.stopPropagation)e.stopPropagation(); if (spot.flying){ var ge = spot.parent.ve.ge; spot.stopFlyAround(); google.earth.addEventListener(ge.getView(), 'viewchangeend', mcpherFlyingClicks(spot,plot)); } else { spot.dealWithClicks(plot); } } |
1 2 3 4 5 6 7 8 |
function mcpherFlyingClicks(spot,plot){ var vm = spot.parent; var ge = vm.ve.ge; if(spot.timer) clearTimeout(spot.timer); spot.timer = setTimeout( function(){ spot.dealWithClicks(plot); }, 100); } |