Search code examples
javascriptcomactivexgoogle-earthgoogle-earth-plugin

Invoking JavaScript Method via COM Interface


I am working on a project to interface Matlab and Google Earth Plugin.

My idea is to use COM interface, in which the MATLAB as COM-Client and Google Earth Plugin in Internet Explorer 9 as COM-Sever.

But until now, I still don't have idea how can I invoke a JavaScript method from MATLAB so that I can update the view of Google Earth in Internet Explorer.

Is it possible to invoke a JavaScript method via COM?

My second idea is to build a custom webbrowser based on InternetExplorer and then embedded custom COM methods and properties, so that it can function with Google Earth regarding the invoking from external application.

Any help would be highly appreciated.

Regards, Wan


Solution

  • Looks like the only supported Google Earth API is the JavaScript API. So your approach of embedding Internet Explorer for this sounds reasonable.

    I'm not so familiar with the MATLAB end of things, but assuming you can embed the web browser control you should then be able to start invoking scripts.

    At the lowest level, the IE web browser implements the IWebBrowser2 interface. This interface exposes a Document property, which returns an IHTMLDocument2 interface. Call IHTMLDocument2::parentWindow to get an IHTMLWindow2 interface.

    Once you have the IHTMLWindow2, supposedly from my reading you have a couple options. Your script can call IHTMLWindow2::execScript. Alternatively, your top-level JavaScript functions should become available as methods on the IHTMLWindow2 interface via the inherited IDispatch: especially useful if you need the return value.

    Some Delphi code that uses execScript: http://www.delphidabbler.com/articles?article=21

    Some .NET Framework code that uses IDispatch directly (see the commented-out more complicated code example): http://www.west-wind.com/weblog/posts/2008/Sep/27/Calling-JavaScript-functions-in-the-Web-Browser-Control

    To make a long story short, essentially you need to do this:

    myWebBrowser.Document.parentWindow.MyJavaScriptFunction()

    or this:

    myWebBrowser.Document.parentWindow.execScript("MyJavaScriptFunction();", "JavaScript")