kaXmlOverlay.js
Summary
No overview generated for 'kaXmlOverlay.js'
Method Summary
|
static Object
|
loadXMLDoc(url)
retrieve XML document as document object
|
function kaXmlOverlay( oKaMap, zIndex )
{
kaTool.apply( this, [oKaMap] );
this.name = 'kaXmlOverlay';
for (var p in kaTool.prototype)
{
if (!kaXmlOverlay.prototype[p])
kaXmlOverlay.prototype[p]= kaTool.prototype[p];
}
this.browser = new _BrowserIdent();
this.urlBase = this.kaMap.server;
this.urlBase += (this.urlBase!=''&&this.urlBase.substring(-1)!='/')?'':'/';
this.ovrObjects = new Array();
this.z_index = zIndex;
this.overlayCanvas = this.kaMap.createDrawingCanvas( zIndex );
this.kaMap.registerForEvent( KAMAP_SCALE_CHANGED, this, this.scaleChanged );
}
kaXmlOverlay.prototype.scaleChanged = function( eventID, mapName ) {
if (this.ovrObjects == null) return;
for (var i=0; i < this.ovrObjects.length; i++) {
this.ovrObjects[i].rescale();
}
}
kaXmlOverlay.prototype.remove = function() {
this.kaMap.deregisterForEvent( KAMAP_SCALE_CHANGED, this, this.scaleChanged );
this.removePoint();
this.kaMap.removeDrawingCanvas(this.overlayCanvas);
}
kaXmlOverlay.prototype.loadXml = function(xml_url) {
this.urlBase = this.kaMap.server;
this.urlBase += (this.urlBase!=''&&this.urlBase.substring(-1)!='/')?'':'/';
this.xmlOvrUrl = this.urlNormalize(xml_url);
var xmlDocument = loadXMLDoc(this.xmlOvrUrl);
var objDomTree = xmlDocument.documentElement;
var dels = objDomTree.getElementsByTagName("delete");
for (var i=0; i<dels.length; i++) {
var a_id = dels[i].getAttributeNode("id");
if (a_id == null) {
this.removePoint();
} else {
this.removePoint(a_id.value);
}
}
var need_update = false;
var points = objDomTree.getElementsByTagName("point");
for (var i=0; i<points.length; i++) {
var a_pid = points[i].getAttributeNode("id");
if (a_pid == null) {
continue;
}
var pid = a_pid.value;
var np = this.getPointObject(pid);
if (np == null) {
np = new kaXmlPoint(pid,this);
this.ovrObjects.push(np);
}
np.parse(points[i]);
need_update = true;
}
if (need_update) this.kaMap.updateObjects();
}
kaXmlOverlay.prototype.urlNormalize = function(url) {
if (url == null) return "";
if (url.substring(1)!='/') {
return this.urlBase+url;
}
return url;
}
kaXmlOverlay.prototype.getDiv = function(pid) {
var div_id = this.getDivId(pid);
return getRawObject(div_id);
}
kaXmlOverlay.prototype.getPointObject = function(pid) {
for (var i=0; i < this.ovrObjects.length; i++) {
if (this.ovrObjects[i] != null && this.ovrObjects[i].pid == pid) {
return this.ovrObjects[i];
}
}
return null;
}
kaXmlOverlay.prototype.addNewPoint = function(pid,x,y) {
this.removePoint(pid);
var np = new kaXmlPoint(pid,this);
np.placeOnMap(x,y);
this.ovrObjects.push(np);
return np;
}
kaXmlOverlay.prototype.getDivId = function(pid) {
return 'xmlovr_'+pid+'_div';
}
kaXmlOverlay.prototype.removePoint = function( pid ) {
if ( (this.removePoint.arguments.length < 1) || (pid == null) ) {
for (var i=0; i < this.ovrObjects.length; i++) {
if (this.ovrObjects[i] != null) {
this.ovrObjects[i].removeFromMap();
delete this.ovrObjects[i];
this.ovrObjects[i] = null;
}
delete this.ovrObjects[i];
}
return;
}
var re = new RegExp(pid);
for (var i=0; i < this.ovrObjects.length; i++) {
if (re.test(this.ovrObjects[i].pid)) {
if (this.ovrObjects[i] != null) {
this.ovrObjects[i].removeFromMap();
delete this.ovrObjects[i];
this.ovrObjects[i] = null;
}
delete this.ovrObjects[i];
}
}
}
function loadXMLDoc(url) {
var xDoc;
var req;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.open("GET", url, false);
req.send();
}
}
if (req) {
if (req.status == 200) {
xDoc = req.responseXML;
if (xDoc && typeof xDoc.childNodes != "undefined" &&
xDoc.childNodes.length == 0) {
xDoc = null;
}
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
} else {
alert("Sorry, this browser isn't equipped to read XML data.");
}
return xDoc;
}
function kaXmlGraphicElement() {}
kaXmlGraphicElement.prototype.parseElement = function(point, domElement) {}
kaXmlGraphicElement.prototype.draw = function(point) {}
kaXmlGraphicElement.prototype.rescale = function(point) {}
function kaXmlSymbol() {
kaXmlGraphicElement.apply(this);
for (var p in kaXmlGraphicElement.prototype) {
if (!kaXmlSymbol.prototype[p])
kaXmlSymbol.prototype[p]= kaXmlGraphicElement.prototype[p];
}
this.shape = "bullet";
this.size = 10;
this.color = "black";
}
kaXmlSymbol.prototype.parseElement = function(point, domElement) {
this.shape = domElement.getAttribute("shape");
this.size = parseInt(domElement.getAttribute("size"));
var c = domElement.getAttribute("color");
if (c != null) this.color = c;
}
kaXmlSymbol.prototype.draw = function(point) {
var jsgObject = new jsGraphics(point.divId);
with (jsgObject) {
setColor(this.color);
var d = this.size / 2;
fillEllipse(-d, -d, this.size, this.size);
paint();
}
}
function kaXmlFeature( point ) {
kaXmlGraphicElement.apply(this);
for (var p in kaXmlGraphicElement.prototype) {
if (!kaXmlFeature.prototype[p])
kaXmlFeature.prototype[p]= kaXmlGraphicElement.prototype[p];
}
this.stroke = 1;
this.color = null;
this.bcolor = null;
this.opacity = 1;
this.cxmin = 0;
this.cymax = 0;
this.coords = "";
this.img = null;
var map = point.xml_overlay.kaMap.getCurrentMap();
var scales = map.getScales();
this.maxScale = scales[scales.length - 1];
this.mcs = point.xml_overlay.kaMap.cellSize / point.xml_overlay.kaMap.getCurrentScale() * this.maxScale;
}
kaXmlFeature.prototype.parseElement = function(point, domElement) {
var t;
t = parseInt(domElement.getAttribute("stroke"));
if (! isNaN(t)) this.stroke = t;
t = domElement.getAttribute("color");
if (t != null) this.color = t;
t = domElement.getAttribute("bcolor");
if (t != null) this.bcolor = t;
t = parseFloat(domElement.getAttribute("opacity"));
if(! isNaN(t)) this.opacity = t;
var text = "";
if (domElement.firstChild != null) {
text = domElement.firstChild.data;
this.readCoordinates(point, text) ;
}
}
kaXmlFeature.prototype.readCoordinates = function(point, text) {
var cx = new Array();
var cy = new Array();
this.cxmin = 0;
this.cymax = 0;
this.coords = "";
var pp = text.split(',');
var i;
for (i=0; i<pp.length; i++) {
var s = pp[i];
var xy = s.match(/[-\+\d\.]+/g);
if (xy != null) {
var x=parseFloat(xy[0]);
var y=parseFloat(xy[1]);
cx.push(x);
cy.push(y);
if (i==0 || x<this.cxmin) this.cxmin = x;
if (i==0 || y>this.cymax) this.cymax = y;
}
}
for (i=0; i<pp.length; i++) {
var x = Math.round((cx[i] - this.cxmin) / this.mcs);
var y = Math.round((this.cymax - cy[i]) / this.mcs);
if (i>0) this.coords += ",";
this.coords += x+","+y;
}
}
function kaXmlLinestring( point ) {
kaXmlFeature.apply(this, [point]);
for (var p in kaXmlFeature.prototype) {
if (!kaXmlLinestring.prototype[p])
kaXmlLinestring.prototype[p]= kaXmlFeature.prototype[p];
}
}
kaXmlLinestring.prototype.draw = function(point) {
var xy = point.xml_overlay.kaMap.geoToPix( this.cxmin, this.cymax );
var x0 = xy[0];
var y0 = xy[1];
xy = point.xml_overlay.kaMap.geoToPix( point.div.lon, point.div.lat );
var xr = xy[0];
var yr = xy[1];
var border = 5;
if (this.img == null) {
this.img = document.createElement( 'img' );
point.div.appendChild( this.img );
this.img.style.position = 'absolute';
}
this.img.style.top = (y0 - yr - border)+'px';
this.img.style.left = (x0 - xr - border)+'px';
var scf = point.xml_overlay.kaMap.getCurrentScale() / this.maxScale;
var it = point.xml_overlay.browser.getPreferredImageType();
var u = "drawgeom.php?gt=L&st="+this.stroke+"&bp="+border+"&sc="+scf+"&cl="+this.coords;
if (this.color != null) u += "&lc="+escape(this.color);
if (it == "P") {
u += "&it=P";
} else {
u += "&it=G";
}
if (point.xml_overlay.browser.getPreferredOpacity() == "server") {
if (this.opacity < 1) u += "&op="+(this.opacity*100);
} else {
if (this.opacity < 1) point.xml_overlay.browser.setOpacity(this.img, this.opacity);
}
this.img.src = u;
}
kaXmlLinestring.prototype.rescale = function(point) {
this.draw(point);
}
function kaXmlPolygon( point ) {
kaXmlFeature.apply(this, [point]);
for (var p in kaXmlFeature.prototype) {
if (!kaXmlPolygon.prototype[p])
kaXmlPolygon.prototype[p]= kaXmlFeature.prototype[p];
}
}
kaXmlPolygon.prototype.draw = function(point) {
var xy = point.xml_overlay.kaMap.geoToPix( this.cxmin, this.cymax );
var x0 = xy[0];
var y0 = xy[1];
xy = point.xml_overlay.kaMap.geoToPix( point.div.lon, point.div.lat );
var xr = xy[0];
var yr = xy[1];
var border = 5;
if (this.img == null) {
this.img = document.createElement( 'img' );
this.img.style.position = 'absolute';
point.div.appendChild( this.img );
}
var scf = point.xml_overlay.kaMap.getCurrentScale() / this.maxScale;
var it = point.xml_overlay.browser.getPreferredImageType();
var u = "drawgeom.php?gt=P&st="+this.stroke+"&bp="+border+"&sc="+scf+"&cl="+this.coords;
if (this.color != null) u += "&fc="+escape(this.color);
if (this.bcolor != null && this.bcolor != "") u += "&lc="+escape(this.bcolor);
if (it == "P") {
u += "&it=P";
} else {
u += "&it=G";
}
if (point.xml_overlay.browser.getPreferredOpacity() == "server") {
if (this.opacity < 1) u += "&op="+(this.opacity*100);
} else {
if (this.opacity < 1) point.xml_overlay.browser.setOpacity(this.img, this.opacity);
}
this.img.style.top = (y0 - yr - border)+'px';
this.img.style.left = (x0 - xr - border)+'px';
this.img.src = u;
}
kaXmlPolygon.prototype.rescale = function(point) {
this.draw(point);
}
function kaXmlLabel() {
kaXmlGraphicElement.apply(this);
for (var p in kaXmlGraphicElement.prototype) {
if (!kaXmlLabel.prototype[p])
kaXmlLabel.prototype[p]= kaXmlGraphicElement.prototype[p];
}
this.text = "";
this.color = "black";
this.boxcolor = null;
this.w = 64;
this.h = 24;
this.xoff = 0;
this.yoff = 0;
this.fsize = "10px";
this.font = "Arial";
}
kaXmlLabel.prototype.parseElement = function(point, domElement) {
if (domElement.firstChild != null) {
this.text = domElement.firstChild.data;
}
var t;
t = domElement.getAttribute("color");
if (t != null) {
this.color = t;
}
this.boxcolor = domElement.getAttribute("boxcolor");
t = parseInt(domElement.getAttribute("w"));
if (!isNaN(t)) {
this.w = t;
}
t = parseInt(domElement.getAttribute("h"));
if (!isNaN(t)) {
this.h = t;
}
t = parseInt(domElement.getAttribute("px"));
if (!isNaN(t)) {
this.xoff = t;
}
t = parseInt(domElement.getAttribute("py"));
if (!isNaN(t)) {
this.yoff = t;
}
t = domElement.getAttribute("fsize");
if (t != null) {
this.fsize = t;
}
t = domElement.getAttribute("font");
if (t != null) {
this.font = t;
}
}
kaXmlLabel.prototype.draw = function(point) {
var x = this.xoff;
var y = this.yoff;
var ldiv = document.createElement( 'div' );
ldiv.style.fontFamily = this.font;
ldiv.style.fontSize = this.fsize;
ldiv.style.textAlign = 'center';
ldiv.style.color = this.color;
ldiv.style.left = x+'px';
ldiv.style.top = y+'px';
ldiv.style.position = 'absolute';
if (this.boxcolor != null) ldiv.style.backgroundColor = this.boxcolor;
if (this.w>0) ldiv.style.width = this.w+'px';
else ldiv.style.whiteSpace = 'nowrap';
if (this.h>0) ldiv.style.height = this.h+'px';
var ltxt = document.createTextNode(this.text);
ldiv.appendChild( ltxt );
point.div.appendChild( ldiv );
}
function kaXmlIcon() {
kaXmlGraphicElement.apply(this);
for (var p in kaXmlGraphicElement.prototype) {
if (!kaXmlIcon.prototype[p])
kaXmlIcon.prototype[p]= kaXmlGraphicElement.prototype[p];
}
this.icon_src = null;
this.icon_w = 0;
this.icon_h = 0;
this.xoff = 0;
this.yoff = 0;
}
kaXmlIcon.prototype.parseElement = function(point, domElement) {
this.icon_src = point.xml_overlay.urlNormalize(domElement.getAttribute("src"));
this.icon_w = parseInt(domElement.getAttribute("w"));
this.icon_h = parseInt(domElement.getAttribute("h"));
var t;
t = parseInt(domElement.getAttribute("px"));
if (!isNaN(t)) {
this.xoff = t;
}
t = parseInt(domElement.getAttribute("py"));
if (!isNaN(t)) {
this.yoff = t;
}
}
kaXmlIcon.prototype.draw = function(point) {
var dx = -this.icon_w / 2 + this.xoff;
var dy = -this.icon_h / 2 + this.yoff;
var idiv = document.createElement( 'div' );
idiv.style.position = 'absolute';
idiv.style.top = dy+'px';
idiv.style.left = dx+'px';
var img = document.createElement( 'img' );
img.src = this.icon_src;
img.width = this.icon_w;
img.height = this.icon_h;
idiv.appendChild( img );
point.div.appendChild( idiv );
}
function kaXmlPoint(pid, xml_overlay) {
this.xml_overlay = xml_overlay;
this.pid = pid;
this.divId = this.xml_overlay.getDivId(pid);
this.geox = 0;
this.geoy = 0;
this.shown = false;
this.graphics = new Array();
this.div = document.createElement('div');
this.div.setAttribute('id', this.divId);
}
kaXmlPoint.prototype.placeOnMap = function( x, y ) {
if (!this.shown) {
this.geox = x;
this.geoy = y;
this.xml_overlay.kaMap.addObjectGeo( this.xml_overlay.overlayCanvas, x, y, this.div );
this.shown = true;
}
}
kaXmlPoint.prototype.removeFromMap = function( ) {
if (this.shown) {
this.xml_overlay.kaMap.removeObject( this.div );
this.shown = false;
}
}
kaXmlPoint.prototype.setPosition = function( x, y ) {
if (this.shown) {
this.geox = x;
this.geoy = y;
this.div.lat = y;
this.div.lon = x;
}
}
kaXmlPoint.prototype.addGraphic = function( obj ) {
this.graphics.push(obj);
obj.draw(this);
}
kaXmlPoint.prototype.clear = function() {
this.div.innerHTML = "";
this.graphics.length = 0;
}
kaXmlPoint.prototype.setInnerHtml = function(ihtml) {
this.clear();
this.div.innerHTML = ihtml;
}
kaXmlPoint.prototype.parse = function(point_element) {
var i;
var x = parseFloat(point_element.getAttribute("x"));
var y = parseFloat(point_element.getAttribute("y"));
var redraw_a = point_element.getAttribute("redraw");
var redraw = false;
if (redraw_a == "true") redraw = true;
if (!this.shown) {
this.placeOnMap(x,y);
this.shown = true;
} else {
this.setPosition(x,y);
if (!redraw) return;
this.clear();
}
var ihtml_element = point_element.getElementsByTagName("ihtml");
for (i=0; i<ihtml_element.length; i++) {
this.div.innerHTML = ihtml_element[i].firstChild.nodeValue;
}
var t;
var elements;
elements = point_element.getElementsByTagName("symbol");
for (i=0; i<elements.length; i++) {
t = new kaXmlSymbol();
t.parseElement(this, elements[i]);
this.addGraphic(t);
}
elements = point_element.getElementsByTagName("icon");
for (i=0; i<elements.length; i++) {
t = new kaXmlIcon();
t.parseElement(this, elements[i]);
this.addGraphic(t);
}
elements = point_element.getElementsByTagName("label");
for (i=0; i<elements.length; i++) {
t = new kaXmlLabel();
t.parseElement(this, elements[i]);
this.addGraphic(t);
}
elements = point_element.getElementsByTagName("linestring");
for (i=0; i<elements.length; i++) {
t = new kaXmlLinestring(this);
t.parseElement(this, elements[i]);
this.addGraphic(t);
}
elements = point_element.getElementsByTagName("polygon");
for (i=0; i<elements.length; i++) {
t = new kaXmlPolygon(this);
t.parseElement(this, elements[i]);
this.addGraphic(t);
}
}
kaXmlPoint.prototype.rescale = function(point_element) {
for (i=0; i<this.graphics.length; i++) {
this.graphics[i].rescale(this);
}
}
function _BrowserIdent() {
this.detect = navigator.userAgent.toLowerCase();
this.OS = null;
this.browser = null;
this.version = null;
this.total = null;
this.thestring = null;
this.place = 0;
if (this.checkIt('konqueror')) {
this.browser = "Konqueror";
this.OS = "Linux";
} else if (this.checkIt('safari')) this.browser = "Safari";
else if (this.checkIt('omniweb')) this.browser = "OmniWeb";
else if (this.checkIt('opera')) this.browser = "Opera"
else if (this.checkIt('webtv')) this.browser = "WebTV";
else if (this.checkIt('icab')) this.browser = "iCab"
else if (this.checkIt('msie')) this.browser = "Internet Explorer"
else if (!this.checkIt('compatible')) {
this.browser = "Netscape Navigator"
this.version = this.detect.charAt(8);
} else this.browser = "An unknown browser";
if (!this.version) this.version = this.detect.charAt(this.place + this.thestring.length);
if (!this.OS) {
if (this.checkIt('linux')) this.OS = "Linux";
else if (this.checkIt('x11')) this.OS = "Unix";
else if (this.checkIt('mac')) this.OS = "Mac";
else if (this.checkIt('win')) this.OS = "Windows";
else this.OS = "an unknown operating system";
}
}
_BrowserIdent.prototype.checkIt = function (string) {
this.place = this.detect.indexOf(string) + 1;
this.thestring = string;
return this.place;
}
_BrowserIdent.prototype.setOpacity = function (imageobject, opacity) {
if (opacity == undefined || this.opacity >= 1) return '';
if (this.browser == "Netscape Navigator")
imageobject.style.MozOpacity=opacity;
else if (this.browser == "Internet Explorer" && parseInt(this.version)>=4) {
var tmp = imageobject.style.cssText;
tmp = "filter: alpha(opacity="+(opacity*100)+");" + tmp;
imageobject.style.cssText = tmp;
} else {
var tmp = imageobject.style.cssText;
tmp = "opacity: "+opacity+";" + tmp;
imageobject.style.cssText = tmp;
}
}
_BrowserIdent.prototype.getPreferredImageType = function () {
if (this.browser == "Netscape Navigator") return "P";
else if (this.browser == "Opera") return "P";
else if (this.browser == "Safari") return "P";
else return "G"
}
_BrowserIdent.prototype.getPreferredOpacity = function () {
if (this.browser == "Netscape Navigator") return "server";
else if (this.browser == "Opera") return "server";
else return "client"
}
Documentation generated by
JSDoc on Wed Mar 15 10:50:59 2006