Select to view content in your preferred language

LabelPoints with text from queryTask

893
4
Jump to solution
08-16-2012 02:52 AM
JanJeske
Deactivated User
I like to lable all geometrys in a query with the name of the owner. The point where the TextSymbol is generatet by the geometryService.labelPoints function. I got an X and Y Koordinate but how i get the Name of the owner in the text symbol?

My Code:
function name_eintragen(ind) {    map.graphics.clear();    //build query task    queryTask = new esri.tasks.QueryTask("http://geoarcgis-srv/dienste/rest/services/alkis/FeatureServer/42");    query = new esri.tasks.Query();    query.returnGeometry = true;    query.outFields = ["id,ownername"];     query.where = "id IN('" + ind + "')";     queryTask.execute(query,function(fset)   {    dojo.forEach(fset.features, function (feature)    {       if(feature.geometry.rings.length > 0)    {              i = feature.geometry.rings.length;     geometryService.simplify([ feature.geometry ],function (geometries,i)      {            if(geometries[0].rings.length > 0)                                         {       geometryService.labelPoints(geometries, function(labelPoints)                                                  {             var font = new esri.symbol.Font("20px",                                                    esri.symbol.Font.STYLE_NORMAL,                                                    esri.symbol.Font.VARIANT_NORMAL,                                                    esri.symbol.Font.WEIGHT_BOLDER);         dojo.forEach(labelPoints, function(labelPoint)                                                   {            var textSymbol = new esri.symbol.TextSymbol('%ownername%', font, new dojo.Color([0, 0, 0]));                       var labelPointGraphic = new esri.Graphic(labelPoint,textSymbol);           // add the label point graphic to the map        map.graphics.add(labelPointGraphic);        globalcounter++;         });       });      }         });    }   });      }); }


The '%ownername%' marks where the name of the owner should stand.

any suggestions?
0 Kudos
1 Solution

Accepted Solutions
__Rich_
Deactivated User
Looking at the code, it would appear that the feature variable is in scope when you're instantiating your TextSymbol so:

feature.attributes["ownername"]

Should just about do it.

Please don't 'leap' for global variables and counters etc. to work around scoping problems even if they do appear to solve the problem, learn about the scope of variables so that you understand where you need to provide more control through things like dojo.hitch.  As a rule of thumb when programming, always keep scope as small as possible this goes hand-in-hand with acquire late, release early for object creation etc.

I haven't reviewed all of your code so there may be other issues or inefficiencies in there, I'd suggest re-writing from scratch using your new found knowledge 🙂

View solution in original post

0 Kudos
4 Replies
__Rich_
Deactivated User
What have you tried so far?

I can see you have a FeatureSet and you're looping through each Feature, a Feature is a Graphic, a Graphic has attributes, so...
0 Kudos
JanJeske
Deactivated User
I have tried do add the ownername to the other functions but they have no callback for that so no success, (maybe i am to stupid?) ...the ownername is under feature.attributes.ownername

Then i tried to store the name in a global variable ...this works fine if i have only one Polygon..but not if i have more. Cause of that the commands run asynchronly.

Then I tried to set a counter tto it store the featureset global and open then right array by the counter same problem-->asynchronly
0 Kudos
__Rich_
Deactivated User
Looking at the code, it would appear that the feature variable is in scope when you're instantiating your TextSymbol so:

feature.attributes["ownername"]

Should just about do it.

Please don't 'leap' for global variables and counters etc. to work around scoping problems even if they do appear to solve the problem, learn about the scope of variables so that you understand where you need to provide more control through things like dojo.hitch.  As a rule of thumb when programming, always keep scope as small as possible this goes hand-in-hand with acquire late, release early for object creation etc.

I haven't reviewed all of your code so there may be other issues or inefficiencies in there, I'd suggest re-writing from scratch using your new found knowledge 🙂
0 Kudos
JanJeske
Deactivated User
Thanks,

now I done it witch hitch and it worked 😉
0 Kudos