[OpenLayers-Users] Changing styleMap of a OpenLayers.Layer.Vector after map initialization?
Andreas Hocevar
ahocevar at opengeo.org
Tue Jun 5 00:35:56 PDT 2012
A much nicer way than using two style maps would be to have a single
style map which determines the fill(?)color by the state of your
button:
var context = {
getColor: function(feature) {
return myButton.active ? "green" : "red";
}
};
var template = OpenLayers.Util.extend({
fillColor: "${getColor}" // using context.getColor(feature)
}, OpenLayers.Feature.Vector.style["default"]);
var style = new OpenLayers.Style(template, {context: context});
var styleMap = new OpenLayers.StyleMap(style);
If you really want to switch style maps, you can do
vectorLayer.styleMap = redStyleMap;
vectorLayer.redraw();
Andreas.
On Sun, Jun 3, 2012 at 1:45 AM, alexe100 <imoveisnacionais at gmail.com> wrote:
> Hi
> I have a map where user can draw polygons to represent geofences. I want
> that geofences where vehicles must be inside to be drawn in green and
> geofences where vehicles can not be inside to be drawn in red.
> I have the styleMapVermelho (red color) and styleMapVerde (green)
> OpenLayers.StyleMap variables.
> So, the application starts with the green color selected. I have a button to
> change the color to red and vice-versa.
>
> The question is, how to remove the current OpenLayers.StyleMap and set the
> other OpenLayers.StyleMap when the button is pressed?
> Please, see code below for init function:
>
> var map, drawControls;
> var polygonLayer;
> var styleMapVerde;
> var styleMapVermelho;
> function init()
> {
> map = new OpenLayers.Map('mapdiv');
>
> var wmsLayer= new OpenLayers.Layer.OSM(); //This uses meters in X/Y!!!
> map.addLayer(wmsLayer);
>
> map.addControl(new OpenLayers.Control.MousePosition());
>
>
>
> //cores para polygon layer
> styleMapVermelho = new OpenLayers.StyleMap(OpenLayers.Util.applyDefaults
> (
> {fillColor: "red", fillOpacity: 0.25, strokeColor: "black"},
> OpenLayers.Feature.Vector.style["default"])
> );
>
> styleMapVerde = new OpenLayers.StyleMap(OpenLayers.Util.applyDefaults
> (
> {fillColor: "green", fillOpacity: 0.25, strokeColor: "black"},
> OpenLayers.Feature.Vector.style["default"])
> );
> polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer",{styleMap:
> styleMapVerde});
> map.addLayer(polygonLayer);
> document.getElementById('dentro').checked=true;
>
>
> drawControls =
> {
> polygon: new
> OpenLayers.Control.DrawFeature(polygonLayer,OpenLayers.Handler.Polygon)
> };
> map.addControl(drawControls['polygon']);
> drawControls['polygon'].activate();
>
>
> map.setCenter(new OpenLayers.LonLat(0, 0), 3);
> }
>
>
>
>
>
> --
> View this message in context: http://osgeo-org.1560.n6.nabble.com/Changing-styleMap-of-a-OpenLayers-Layer-Vector-after-map-initialization-tp4978765.html
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
--
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.
More information about the Users
mailing list