[OpenLayers-Users] Add a label on an Open Layers feature dynamically

thanili ailiop at ics.forth.gr
Wed Mar 12 07:38:41 PDT 2014


Guys i need some help on how to add (using maybee a popup requesting info
from the user) on the fly labels: 

/function initialiseMap(){ 
    ... 
    //map image layer 
    imageLayer = new OpenLayers.Layer.TMS(imgURL, "", { 
        url : '', 
        serviceVersion : '.', 
        layername : '.', 
        alpha : true, 
        type : 'png', 
        getURL : overlay_getTileURL, 
        transitionEffect: 'resize' 
    }); 
    map.addLayer(imageLayer); 
    //prepare vector layer upon which features can be drawn 
    map.addLayer(imageLayer); 
    var vlayer = new OpenLayers.Layer.Vector("Editable"); 
    map.addLayer(vlayer); 
    //fetch previously saved layer 
    var previousFeatures = getJSONData(); 
    if(previousFeatures!=null) { 
    vlayer.addFeatures(previousFeatures); 
    } else { 
    } 
    ... // draw controls - toolbar, zoom controls etc// ... 
    var drawRegularPolygonControl = new
OpenLayers.Control.DrawFeatureOpt(vlayer, OpenLayers.Handler.RegularPolygon, 
            {title:'Draw a regular polygon', text: 'Regular
Polygon','displayClass':'olControlDrawFeatureRegularPolygon'}); 
    ... 
    drawRegularPolygonControl.events.register("featureadded", this,
function(e) { 
        modifyFeatureControl.selectFeature(e.feature); 
        modifyFeatureControl.selectControl.select(e.feature); 
        modifyFeatureControl.activate(); 
        drawControl.deactivate(); 
    }); 
    .../

All features can be drawn, saved and subsequently fetched in a vector layer.
I would like to have the option to actually add a label to each such
feature. 

As far i have looked at OpenLayers examples or read OpenLayers Docs i have
not found any relevant example, apart from this: 

http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/vector-features-with-text.html

however this example is static. I use OpenLayers.Control,
OpenLayers.Handlers to actually draw dynamically features. 

How could i implement controls for adding feature labels? I guess 

I am thinking of using a control and a corresponding event like: 

/var addLabelFeatureControl = new OpenLayers.Control.SelectFeature(vlayer, { 
    clickout: false, 
    toggle: false, 
    title: 'Add Label', text: 'Add Label to Feature', 
    displayClass: "olControlAddLabel" 
}); 
... 
addLabelFeatureControl.events.register("featurelabeled", this, function(e) { 
    var tempfeature = modifyFeatureControl.selectFeature(e.feature); 
    tempfeature. 
    modifyFeatureControl.selectControl.select(e.feature); 
    modifyFeatureControl.activate(); 
    drawControl.deactivate(); 
});/

A) Is this approach correct? and if so B) How am i going to manipulate
tempfeature? Meaning of terms of displaying a popup, getting user info
(feature title,description) and saving into an existing or a new layer? C)
If not could somebody suggest an approach?



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Add-a-label-on-an-Open-Layers-feature-dynamically-tp5128562.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.


More information about the Users mailing list