[Users] Need help to dynamically add vector layer to treePanel
David E. Reksten
der at dod.no
Mon Sep 20 17:02:43 CEST 2010
Hi all,
after having spent a lot of time trying different strategies, I still
can't for the life of me figure out how to dynamically add a vector
layer to my layer tree.
What I have so far:
mapPanel = new GeoExt.MapPanel({
border: true,
region: "center",
map: new OpenLayers.Map(myMapConfig);,
margins: '0 5 5 0',
layers: [baseLayer1, baseLayer2, baseLayer3]
});
treeConfig = new OpenLayers.Format.JSON().write([{
nodeType: "gx_baselayercontainer",
text: "Base layers",
expanded: true
}, {
nodeType: "gx_overlaylayercontainer",
text: "My vector layers", // I want the new vector layers to
appear here...
expanded: true,
loader: {
baseAttrs: {
radioGroup: "foo",
uiProvider: "use_radio"
}
}
}], true);
treePanel = new Ext.tree.TreePanel({
id: 'mainpanel',
border: true,
region: "west",
title: "Map layers",
width: 200,
split: true,
collapsible: true,
margins: '0 0 5 5',
collapseMode: "mini",
autoScroll: true,
loader: new Ext.tree.TreeLoader({
applyLoader: false,
uiProviders: {
"use_radio": LayerNodeUI
}
}),
root: {
nodeType: "async",
children: Ext.decode(treeConfig)
},
rootVisible: false,
lines: false
});
The user is supposed to add one or several named layers (by clicking a
button that triggers the following):
function addVectorLayer() {
Ext.MessageBox.prompt('New layer', 'Type name for layer:',
function (btn, text) {
if (btn == 'ok') {
var newVectorLayer = new OpenLayers.Layer.Vector(
text,
{
projection: new OpenLayers.Projection("EPSG:4326"),
displayInLayerSwitcher: false,
isBaseLayer: false,
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style(
OpenLayers.Util.applyDefaults({
fillColor:"#FF6666",
strokeColor: "#FF3333",
strokeWidth: 2
},
OpenLayers.Feature.Vector.style["default"])
)
})
);
// ***** THIS IS WHERE IT FAILS ******
map.addLayer(newVectorLayer); // this works, but does
not add the layer to my treePanel
mapPanel.layers.add(newVectorLayer); // ...so I tried
this but get an exception
};
}
);
};
I have looked and looked at the example in
http://dev.geoext.org/trunk/geoext/examples/layercontainer.js but
still can't get it to work and I guess that I must be missing
something basic in how I use these components. For instance, my gut
feeling tells me that the treePanel object should probably be told
about the mapPanel somewhere, or is there some magic going on behind
my back?
Any help with this is greatly appreciated!
Regards,
.david
More information about the Users
mailing list