It is possible for worldKit to respond to certain commands from Javascript, and for worldKit to send commands to Javascript. This allows for interesting integrations within web applications.
For Javascript-Flash communication in either direction, make sure that
swLiveConnect="true" is set in the
embed HTML tag.
It is highly recommended that applications needing Javascript communication use the latest worldKit and require Flash Player 8, as it fixes browser inconstencies.
Javascript communicates with Flash by calling the
SetVariable function on worldKit embedded in the browser. "document.worldkit" refers to the "id" and "name" assigned in the
<object> and <embed> tags.
document.worldkit.SetVariable(Variable,Value);
These are variables worldKit monitors and responds to:
- JSubComm: Annotations categorized under the value of this variable will be activated.
- JLayComm: Annotations categorized under the value of this variable will be switched from visible to invisible, or vice versa.
- JActComm: Annotations categorized under the value of this variable will set to the color speicifed in <activatecolor>.
- JComm: The annotation assigned the id specified in the value of this variable will be activated..
- JItemActComm: The annotation assigned the id specified in the value of this variable will be set to the color specified in <activatecolor>.
- JLoadComm: Set the value to trigger worldKit to load an RSS feed. Pass "dataurl" or "updateurl" to load feeds specified in the config.xml. Pass "clear" to remove all the current annotations. Or alternatively, pass a url to load an arbitrary RSS feed.
- JRSSComm: Set the value to a string containing RSS. worldKit will parse this RSS string, and display any annotations.
- JZoomComm: Set the value to a location and zoom level, and worldKit will zoom and pan to that location. The format is "lat,lon,zoomlevel".
- JInputComm: Set to "true" to turn on Input-Only Mode. Set to "false" to switch off.
- JNavmodeComm: Set the value to "zoomselect" to activate the red selection box tool, and to "grabber" to activate the click and drag tool.
- JGetItemComm: The annotation assigned the id specified in the value of this variable will pass its contents, each field seperated by "||", through the URL or Javascript function specified in <annotateurl>.
- JGetExtentComm: Pass the name of a javascript function, which will be called with the current extent of the map view, similar to "onzoompan" below.
For example, here are snippets of Javascript and HTML using "JSubComm" and "JLayComm" that create a checkbox to switch "layers" on and off, in a GIS style, and a link to activate a category of points.
<script>
function activate(args) {
document.worldkit.SetVariable("JSubComm",args);
}
function layerSwitch(args) {
document.worldkit.SetVariable("JLayComm",args);
}
</script>
<input type="checkbox" name="restaurant" checked onMouseUp="javascript:layerSwitch('restaurant')">
<a href="javascript:activate('restaurant')"><font color="#FFFF44">Restaurants</font></a>
User clicks on annotations, or clicks in annotation-input mode, can trigger Javascript code, rather than opening a URL. Javascript callbacks can also be set for zoom/pan, new RSS items, or RSS items expired by "fading". This is useful for filling out forms, loading pictures, etc. without reloading the browser, or precisely controlling the parameters of a new window, or many other tight integrations with web applications.
In each case, worldKit will call a specified function with one argument, args, a string containing kay/value pairs URI encoded.
- To enable Javascript for user clicks on annotations, set <window> to "_javascript:", followed by the name of a callback function. args containts the "lat" and "long" of the click.
- To enable Javascript for Input Mode, set <annotateurl> to "javascript:" followed by the name of a callback function. It returns the "lat", "long", "id", and "link" of the annotation.
- To set a callback for zoom/pan events, set <onzoompan> to "javascript:" followed by the name of a callback function. args contains only the W,S,E,N "extent" of the current view.
- Set <newitemcallback> to "javascript:" followed by the name of a callback function, to receive notification of new RSS items. It returns the "title", "url", "summary", "lat", "lon", and "id" of the annotation.
- Set <fadeditemcallback> to "javascript:" followed by the name of a calllback function, to receive notification of "faded" RSS items. It returns only the "id" of the annotation.
The example code below demonstrates how to receive (myCallback) and parse (parseArgs) messages from worldKit. In the config.xml,
<window> would be set to "_javascript:myCallback".