Link Google Earth Plugin to OpenLayers
I just pushed out GeoPortal v1.3 with some new features - moved to jQuery Templates for reporting, put a lot of config code in one file (contributed by Mike Wilson - thanks!), added search help, and some general bolt tightening.
One of the changes for this release was adding a Google Earth widget to the Google Maps module. I’ve been frowning at the Google Earth plugin for a while now due to it’s lack of Linux support (Google Earth runs fine on Linux for pete’s sake), but since it gives a fairly polite “We don’t support Linux yet” message and it isn’t central to the apps core functionality, I figured I’d toss it in.
On to the code. First, we need a routine to initialize the map. Fortunately I was in decent coding form when I made this routine for Streetview, so it adding Google Earth wasn’t so bad.
1 | /** |
Basically you’re passing the function the type of map you want to control (in this case “Google Earth”), a longlat that represents the OpenLayer’s map center, and an optional reset parameter. The reset parameter is handy - I’ve found hiding streetview or Google Earth widgets and then making them visible again in a jQuery UI Accordion can have unexpected consequences.
So we initialize the Google Earth instance if it either doesn’t exist or we pass the reset parameter, and then hit another function to zoom. If it does exist and reset is false, we just zoom. The reason it’s set up that way is Google Earth loads asynchronously, and it’ll blow right past your createInstance and keep on trucking.
The zoom function is pretty straight forward:
1 | /** |
The things to note here are setTilt and the setRange. SetTilt you should set to whatever makes you happy. I’m not thrilled with setRange. Basically I take the OpenLayers map scale and divide it by 40 (arbitrary number) to get a GE range value I liked, so it zooms in and out with the OpenLayers map. You’ll probably want to tweak that too.
We have to tell OpenLayers to call widgetMaps when the map is done with a move event.
1 | // Hook streetview to map move event |
The mapactive bit is checking to see if the particular widget map is visible.
In this case I’m initializing the widget map when it’s jQuery UI accordion is activated, which is as easy as:
1 | /* Run Code When Active Map Accordion Changes */ |
And…done. Our little Google Earth plugin widget will follow the OpenLayers map around the screen. I haven’t integrated this functionality with any live sites yet, so here’s a quick video showing how it works.
You can download the latest GeoPortal release here.