|
|
When choosing a layer in the pulldown menu on the left a red rectangle with a yellow border is added to the map just above the chosen layer. The rectangle is defined in an a external file, it holds the rectangle definition in SVG syntax but without any header or <svg> root element. You can add the rectangle several times, note that we provided no removing functionality here in order to keep the example simple. These methods are useful for adding predefined map layers that you want to load later or any stuff you may get from a database. |
Right-click, save ...
A script (described below) within the SVG imports an external file, parses it and adds it to the chosen layer. The scripts need to be within the SVG file as the necessary functions require the internal Adobe scripting engine.
The file we add to the graphic needs to be some SVG code in order to get rendered properly. It can be a full SVG file, but note that you have to deal with viewboxes then. The file we add is a truncated SVG file, but still some XML, so we called it get_parse_rect.xml. It holds nothing more than this code:
<rect x="-2461" y="555" width="4625" height="800"
fill="red" fill-opacity=".7" stroke="yellow"
stroke-width="77" stroke-opacity="1"/>
The functions use Adobe viewer internal mechanisms, so the function needs to be within the embedded SVG file. A simple trick helps us to call this function from the outside HTML. We create reference to the script at top (hoping that nobody embeds this page into a frameset).
top.AddMe = addMe;
That way the function defined within the SVG can be called as addMe(), from HTML it can be called as AddMe(). Note the difference in naming (upper and lower case "A"), without that some browsers (MSIE) get confused. But you can use any name you like here.
When choosing a layer in the pulldown menu AddMe(LAYER_NAME) gets called, that is identical to addMe(LAYER_NAME) from within the SVG. Let's have a look at the function itself:
function addMe(fileName,where) {
getURL(fileName, fileLoaded);
function fileLoaded(data) {
var string = '';
if(data.success) {
string = data.content;
} else {
return;
}
var node = parseXML(string, document);
document.getElementById(where).appendChild(node);
}
}
getURL() and parseXML() are predefined functions form the Adobe viewer scripting engine. getURL() requires a callback method to be passed as a parameter. The callback method is passed to an object (here data), which gives some information on the status of the request and the data received (here data.success [true/false] and data.content [the things we need]).
This data.content is stored as a string and passed to parseXML(). The parsing needs to be done somewhere, easiest way: within the document. At this time node holds information similar to the one when you create SVG elements with scripting. Then the string needs to be attached, here at the end of the group or layer chosen from the pulldown menu.
Note: in order to have this script running on Netscape4.x, it needs some slight modifications:
var whereNN;
function addMe(fileName,where) {
whereNN = where;
getURL(fileName, fileLoaded);
function fileLoaded(data) {
var string = '';
if(data.success) {
string = data.content;
} else {
return;
}
var node = parseXML(string, document);
document.getElementById(whereNN).appendChild(node);
}
}
For some reason the where variable is not remembered by the inner function so we pass it to a global variable called whereNN.
You may also add XML directly to the SVG document with this function
top.AddXML = addXML;
function addXML(xml2parse,where){
document.getElementById(where).
appendChild(parseXML(xml2parse,document));
}
You can call it here with this link, a green rectangle should be added to the map. the function call looks like that:
AddXML('<rect x=\'-2261\' y=\'755\' width=\'4625\' height=\'800\'
fill=\'red\' fill-opacity=\'.7\' stroke=\'yellow\'
stroke-width=\'77\' stroke-opacity=\'1\'/>','schrift');
This page borrows ideas from following pages. Please check also those for more information about getURL/parseXML. Note that there is also a postURL method to get information sent back to the server.
| Last modified:
Tuesday, 15-Feb-2005 11:42:05 CET
© carto:net (andré m. winter & andreas neumann) original URL for reference: http://www.carto.net/papers/svg/samples/get_parse.shtml |