Hi,
maybe I have mixed something up. Here is the JS-code of the page:
--------------------------------------------------------
var map;
var wms;
var vectors;
var selectedId = -1;
var popup;
function init(createDynamic) {
map = new OpenLayers.Map('map', {
controls: [
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Navigation(),
]
});
wms = new OpenLayers.Layer.WMS("OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'}
);
var testStyleMap = new OpenLayers.StyleMap({
"default": {
externalGraphic: "/MapTest/scripts/img/marker-green.png",
graphicHeight: 20,
graphicYOffset: -19,
rotation: "${rot}"
},
"select": {
cursor: "crosshair",
externalGraphic: "/MapTest/scripts/img/marker.png"
}
});
if (createDynamic) {
/*
* Create set of features dynamically
*/
var features = new Array(25);
for (var i=0; i<features.length; i++) {
features[i] = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(
(340 * Math.random()) - 170,
(160 * Math.random()) - 80
), {
rot: 100 * Math.random() | 0,
name: "no name",
description: "no desc"
}
);
}
vectors = new OpenLayers.Layer.Vector("Points", {styleMap:
testStyleMap});
vectors.addFeatures(features);
} else {
/*
* Download features from server
*/
vectors = new OpenLayers.Layer.Vector("Points",
{
strategies: [new OpenLayers.Strategy.BBOX({resFactor:
1.1})],
styleMap: testStyleMap,
protocol: new OpenLayers.Protocol.HTTP({
url: "/MapTest/FeatureServlet",
format: new OpenLayers.Format.Text()
})
}
);
}
map.addLayers([wms, vectors]);
// Interaction; not needed for initial display.
selectControl = new OpenLayers.Control.SelectFeature(vectors);
map.addControl(selectControl);
map.setCenter(
new OpenLayers.LonLat(-71.147, 46.472).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 8
);
selectControl.activate();
vectors.events.on({
'featureselected': onFeatureSelect,
'featureunselected': onFeatureUnselect
});
};
// Needed only for interaction, not for the display.
function onPopupClose(evt) {
try {
// 'this' is the popup.
selectControl.unselect(this.feature);
} catch (e) {}
}
function onFeatureSelect(evt) {
try {
feature = evt.feature;
popup = new OpenLayers.Popup.FramedCloud("featurePopup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(100,100),
"<h2>" + feature.attributes.name + "</h2>" +
feature.attributes.description + "<br/>" +
"Rotation: <b>" + feature.attributes.rot + "</b>",
null, true, onPopupClose);
feature.popup = popup;
popup.feature = feature;
map.addPopup(popup);
selectedId = feature.attributes.id;
} catch (e) {}
}
function onFeatureUnselect(evt) {
try {
feature = evt.feature;
if (feature.popup) {
popup.feature = null;
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
} catch (e) {}
}
--------------------------------------------------------
If I call init(true), I get rotated icons, if I call init(false), they are not
rotated. In both cases, the label is displayed correctly.
Do you know any online examples that try to rotate a image, while loading the
features dynamically from a server?
All the best,
Heiner
Am 10.11.2010 um 18:04 schrieb Arnd Wippermann:
> Hi,
>
> It should work.
>
> The styleMap of the layer is used, when new features are added to the layer.
> If your attributes match the rules in your stylemap, then the features
> should displayed in that way without any further tasks.
>
> Arnd
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Heiner
> Lamprecht
> Gesendet: Dienstag, 9. November 2010 22:04
> An: [email protected]
> Betreff: [OpenLayers-Users] Using style depending on feature attributes
>
> Dear,
>
> I'm quite new to OpenLayers, started with some small tests last weekend.
> So, please forgive me, if I ask some stupid questions ;-)
>
> At the moment, I want to change the style of a feature according to some
> attribute values. I looked into the examples (like style-rules.html and
> styles-rotation.html), and it looks quite good. My only problem is: How to
> use this with dynamically loaded data.
>
> If I create the features like in the examples (JS-Code in the page) and add
> them to the layer ("vector.addFeatures(features);"), it works. But I would
> like to load the features from a server, using the text format
> (tab-seperated):
>
> var vector = new OpenLayers.Layer.Vector("Points", {
> strategies: [new OpenLayers.Strategy.Fixed()],
> styleMap: new OpenLayers.StyleMap(style),
> protocol: new OpenLayers.Protocol.HTTP({
> url: "/MapTest/FeatureServlet",
> format: new OpenLayers.Format.Text()
> })
> });
>
> I can use the feature attributes f.e. in a layer, so in general, they are
> created correctly.
>
> Any hints what is going wrong? Any additional info needed?
>
>
> Best regards,
>
>
> Heiner
>
> _______________________________________________
> Users mailing list
> [email protected]
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
_______________________________________________
Users mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/openlayers-users