/*
*
* @(#) $Id: kmloverlay.js,v 1.2 2010/09/07 22:49:18 mlemos Exp $
*
*/
if(typeof(ML) === 'undefined')
ML = { }
if(typeof(ML.Maps) === 'undefined')
ML.Maps = { }
if(typeof(ML.Maps.KMLOverlay) === 'undefined')
{
ML.Maps.KMLOverlay = function(url, options)
{
this.url = url;
this.markers = [];
this.placemarks = [];
this.styles = [];
this.icons = ((options && options.icons) ? options.icons : []);
this.parseKML = function(kml)
{
var placemarks = kml.getElementsByTagName('Placemark')
for(var p = 0; p<placemarks.length; p++)
{
this.placemarks[p] = { };
this.placemarks[p].name = placemarks[p].getElementsByTagName('name')[0].childNodes[0].nodeValue;
this.placemarks[p].description = placemarks[p].getElementsByTagName('description')[0].childNodes[0].nodeValue;
this.placemarks[p].styleurl = placemarks[p].getElementsByTagName('styleUrl')[0].childNodes[0].nodeValue;
var coordinates = placemarks[p].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue;
var c = coordinates.split(',');
this.placemarks[p].longitude = parseFloat(c[0]);
this.placemarks[p].latitude = parseFloat(c[1]);
}
var styles = kml.getElementsByTagName('Style')
for(var s = 0; s<styles.length; s++)
{
var style = '#' + styles[s].getAttribute('id');
var icons = styles[s].getElementsByTagName('Icon');
if(icons.length > 0)
{
var href = icons[0].getElementsByTagName('href')[0].childNodes[0].nodeValue;
this.styles[style] = { image: href };
}
}
}
this.addplacemarks = function()
{
for(var p = 0; p < this.placemarks.length; p++)
{
var options = { };
var style = this.placemarks[p].styleurl;
if(this.styles[style])
{
if(!this.icons[style])
{
this.icons[style] = new GIcon(G_DEFAULT_ICON, this.styles[style].image );
this.icons[style].iconSize = new GSize(32,32);
this.icons[style].shadowSize = new GSize(59,32);
this.icons[style].dragCrossAnchor = new GPoint(2,8);
this.icons[style].iconAnchor = new GPoint(16,32);
}
options.icon = this.icons[style];
}
this.markers[p] = new GMarker(new GLatLng(this.placemarks[p].latitude, this.placemarks[p].longitude), options);
this.markers[p].kmlinformation = '<b>' + this.placemarks[p].name + '</b><br />' + this.placemarks[p].description;
GEvent.addListener(this.markers[p], 'click', function() { this.openInfoWindowHtml( this.kmlinformation );});
this.map.addOverlay(this.markers[p]);
}
}
this.initialize = function(map)
{
var self = this;
this.map = map;
_IG_FetchXmlContent(this.url, function(kml)
{
self.parseKML(kml);
self.addplacemarks();
}
);
}
this.redraw = function(force)
{
}
this.remove = function()
{
for(var m = 0; m < this.markers.length; m++)
this.map.removeOverlay(this.markers[m]);
}
this.copy = function()
{
return new ML.Maps.KMLOverlay(this.url);
}
this.prototype = new GOverlay();
}
}
|