I'm a newbie using OpenLayers with JQuery to display vector data from KML files. I'm creating the layer like this:
newlayer = new OpenLayers.Layer.Vector( layerid, {
projection: MAPVAR.displayProjection,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: layerurl,
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true,
maxDepth: 2
})
})
});
I want to avoid using these vector features with the OpenLayers.Control.SelectFeature control. Instead i would like to have each svg feature respond to a click event and pop open their own individual windows with the attributes.
I'm having trouble figuring out how to add the onclick function to ALL the vector features loaded.
For example, I can attache a function to the KML layer's "loadend" event and add the onclick function doing the following:
$('g[id$=vroot] > *').css("cursor", "pointer")
$('g[id$=vroot] > *').click(function(evt){ $w = $.WM_open(); $w.find('.windowcontent').append("SUCKER"); });
But this will only apply the handler to those vector elements in the view extent and not the ones that are waiting to be rendered in the HTML. I'm not sure how to access the unrendered ones -- they are in the DOM.
I've come up with really stupid stupid work around to this problem, but I know there has to be a better way.
Is there some way I can dynamically create the "onclick" handler for each svg feature when they are getting parsed? Would that require me to subclass OpenLayers.Layers.Vector or OpenLayers.Formats.KML? Does anyone have an example? Or recommendation on other ways?
thx mazery
I still have not solved adding the onclick handler to the features. But i was able to override OpenLayers.Format.KML parseData() to add a pointer cursor to the feature styles before the loadend runs on the KML layer (additions demarcated with * EDITS *).
var FormatErmaKml = OpenLayers.Class(OpenLayers.Format.KML, {
parseData: function (data, b) {
typeof data == "string" && (data = OpenLayers.Format.XML.prototype.read.apply(this, [data]));
for (var c = ["Link", "NetworkLink", "Style", "StyleMap", "Placemark"], d = 0, e = c.length; d < e; ++d) {
var f = c[d];
var g = this.getElementsByTagNameNS(data, "*", f);
if (g.length != 0) switch (f.toLowerCase()) {
case "link":
case "networklink":
this.parseLinks(g, b);
break;
case "style":
this.extractStyles && this.parseStyles(g, b);
break;
case "stylemap":
this.extractStyles && this.parseStyleMaps(g, b);
break;
case "placemark":
this.parseFeatures(g, b)
}
}
**** EDITS BEGIN ****
for(var count = 0; count < this.features.length; ++count){
f = this.features[count];
f.style.cursor = "pointer";
}
**** END EDITS ****
return this.features
},});
Any ideas on adding the onclick handler for each feature are welcomed.
thx mazery
cursor: "pointer". – CaptDragon Jan 19 '12 at 17:39