[OpenLayers-Users] styling related question

Lammie Jonson jrubiando at gmail.com
Tue Dec 8 12:22:54 EST 2009


I have some functionality where the user clicks on some shapes displayed
through WFS. The click then
propogates to the server and it sends back info to the client on how to
makes those shapes appear as highlighted
by simply displaying them in a different set of layers. These layers have
been styled with blue, red, or green colors. I worked out this from some
examples, but the question is if there was a better way to do this. For one
thing, do I need a background layer to style an object or can that object
style itself ?

 The functions below are in the init() function and when the data comes back
from the server it will get passed to highlight_tiles() farther below. I did
not have much luck styling the WFS object either as I wanted to see if I
could get it to display some other color besides the default yellow. I
didn't put the WFS code in here to see however ..



//--------------------------------------

       function get_style(color,opacity,sel_color)
       {
        if (sel_color == null){
          sel_color = "#0000FF";
        }

        var sty = new OpenLayers.StyleMap({
              'default' : new OpenLayers.Style({
               fillColor: color
              ,fillOpacity: opacity
              ,pointRadius: 4
              ,strokeColor: color
              ,strokeWidth: 2
              })
              ,'select': new OpenLayers.Style({
              fillColor: sel_color
             ,fillOpacity: 0.4
             ,strokeColor: sel_color
             })});
        return(sty);
        }




            sel_vectors = new Array();

            var sel_vecstyles = new Array();
            vec_colors = ['BLUE','GREEN','RED'];

            for (var i = 0; i < vec_colors.length; i++)
              {
              var vstyle = get_style(vec_colors[i],1.0);

              var vec = new OpenLayers.Layer.Vector(vec_colors[i] + "
Vector",
                {
                styleMap: vstyle
                }
               );
              sel_vectors[i] = vec;
              map.addLayer(sel_vectors[i]);

            }



// ----------------------------------





        function highlight_tiles(geo_ar)
        {
        for (var i=0;i<geo_ar.length;i++)
          {
           geo = geo_ar[i];
           var pointList = geo.poly;
           vec_idx = i % sel_vectors.length;
           // alert(vec_idx.toString());
           addTile(pointList, geo.id,'object',sel_vectors[vec_idx]);
          }
        }


        // typ is the type of each element of pointList, either array
        // or object.

        function addTile(pointList, id, typ, vec)
          {
           var plist = [];
           for(i = 0; i < pointList.length; i++) {
              var pnt = pointList[i];
              // alert(pnt);

              var gpnt;
              if (typ == 'array')
                {
                gpnt = new OpenLayers.Geometry.Point(pnt[0],pnt[1]);
                }
              else
                {
                if (i == 0) {
                 // alert(pnt.lon);
                }
                gpnt = new OpenLayers.Geometry.Point(pnt.lon,pnt.lat);
                }
              plist.push(gpnt);
            }
            var linearRing = new OpenLayers.Geometry.LinearRing(plist);



            var polygonFeature = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Polygon([linearRing]),
            {id: id});
            vec.addFeatures([polygonFeature]);
          }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20091208/3e852d74/attachment.html


More information about the Users mailing list