[OpenLayers-Users] featureadded

Peter Peterse peter at peterse-uithuizen.com
Fri Oct 7 11:09:11 EDT 2011


Try the next:


>     var proj = {
>         "google": new OpenLayers.Projection("EPSG:900913"),
>         "latlng": new OpenLayers.Projection("EPSG:4326")
>     };
>     map = new OpenLayers.Map('map', {
>         "maxExtent": new OpenLayers.Bounds(-20037508, -20037508, 20037508,
> 20037508),
>         "maxResolution": 156543.0339,
>         "proj": proj,
>         "units": "meters"
>     });
>     map.addLayer(new OpenLayers.Layer.Google("Google Physical", {type:
> google.maps.MapTypeId.TERRAIN}));
>
>     map.setCenter(new OpenLayers.LonLat(-89, 43).transform(proj.latlng,
> proj.google), 2);
>

    var context={
       az : function(feature) {
          return feature.attributes.az;
       },
       rate : function(feature) {
          return feature.attributes.rate;
       }
    };
    var template = {
       externalGraphic: "arrow.png",
       rotation: "${az}",
       graphicWidth:6,
       graphicHeight: "${rate}"
    }
    var styleMap = new OpenLayers.StyleMap(new OpenLayers.Style(template,
{context:context}));

    var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry",
{styleMap: styleMap});

    // create a point feature
    var points = [];
    var p = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Point(-72, 42).transform(proj.latlng,
proj.google));

    p["styleMap"] = styleMap;
    p.attributes = {};
    p.attributes["az"] = 76;
    p.attributes["rate"] = 33;
    points.push(p);


>
>     vectorLayer.addFeatures(points);
>
>     vectorLayer.events.on({
>         "featureadded": function(evt) {
>             feature = evt.feature;
>             var str = "";
>             for (var i in feature.attributes) {
>                 str += "<b>" + i + ":</b> " + feature.attributes[i] + "<br
> />";
>             }
>             map.addPopup(new OpenLayers.Popup.FramedCloud(
>                 "featurePopup",
>                 feature.geometry.getBounds().getCenterLonLat(),
>                 new OpenLayers.Size(50, 30),
>                 str,
>                 null,
>                 true,
>                 null
>             ));
>         }
>     });
>     map.addLayer(vectorLayer);


I think it should work.

Regards,
Peter



More information about the Users mailing list