Search code examples
javascriptmatlabgoogle-earthgoogle-earth-plugin

wait for Google Earth to finish loading before executing next command in matlab


I am working on feeding Matlab's simulation data to Google Earth Plug-in via COM.

My problem is that the command,which should be invoked after Google Earth finished loading, is invoked before that. That brings of course error.

I could use the pause command to pause the code waiting the Google Earth to load. But, this solution is not that efficient, as I don't know exactly how fast or how slow Google Earth will load on different machines.

I've also tried using the properties of the COM object. It was close, but no cigar. The code looks like this

waitfor(h.Document.parentWindow.document,'readyState','complete')

or also this one:

while strcmp(h.Document.parentWindow.document.readyState,'complete')== 0
    pause(1);

end  

Is there any object properties that could be used? Thanks!


Solution

  • Found the solution!

    Google Earth Plug-in will call the "initCallback" method when it finished loading.

    By adding a line on "initCallback" method, I change the title of my html document to other name, which indicates that the plugin is loaded.

    function initCallback(pluginInstance) {
          ge = pluginInstance;
          ge.getWindow().setVisibility(true);
    
          // tell the application the plugin is ready
          //(window.external.JSInitSuccessCallback_(pluginInstance);
          document.title = "Google Earth Plugin - Ready";
    
          // prevent mouse navigation in the plugin
          ge.getOptions().setMouseNavigationEnabled(false);
        }
    

    At MATLAB's end, I just added the a while loop, comparing the html document title, pausing the executing until the plugin is finished loading.

    while strcmp(h.Document.title,'Google Earth Plugin - Ready')~=1
        pause(0.01)
    end
    

    Maybe there is other more elegant solution, love to hear your feedbacks