Hi all,
My ultimate goal to add callout text to my selected features with an incremental value like "sale 1", "sale 2"," sale 3" ect... What I have right now is to set up label to display label in callout text and use this snippet to turn the label on for selected features. My issue is my label doesn't come from attribute table and each label is different from the others. How do I modify this code to make it work or if this approach doesn't work in my case, what else could I use to accomplish it? Thank you very much.
<SPAN class="comment token">//Layer to label. US Cities lyr was used</SPAN>
<SPAN class="keyword token">var</SPAN> lyrOfInterest <SPAN class="operator token">=</SPAN> MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetLayersAsFlattenedList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>OfType<SPAN class="operator token"><</SPAN>FeatureLayer<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">FirstOrDefault</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> selection <SPAN class="operator token">=</SPAN> MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetSelection</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//Get the OIDs of the selected features</SPAN>
<SPAN class="comment token">//We will use this to label those features only</SPAN>
<SPAN class="keyword token">var</SPAN> selectedOIDs <SPAN class="operator token">=</SPAN> selection<SPAN class="punctuation token">[</SPAN>lyrOfInterest<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//Get the layer's definition</SPAN>
<SPAN class="keyword token">var</SPAN> lyrDefn <SPAN class="operator token">=</SPAN> lyrOfInterest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetDefinition</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> CIMFeatureLayer<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>lyrDefn <SPAN class="operator token">==</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//Get the label classes - we need the first one</SPAN>
<SPAN class="comment token">//Or you could create a new label class</SPAN>
<SPAN class="keyword token">var</SPAN> listLabelClasses <SPAN class="operator token">=</SPAN> lyrDefn<SPAN class="punctuation token">.</SPAN>LabelClasses<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> theLabelClass <SPAN class="operator token">=</SPAN> listLabelClasses<SPAN class="punctuation token">.</SPAN><SPAN class="token function">FirstOrDefault</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//set the label class Expression to use the Arcade expression</SPAN>
theLabelClass<SPAN class="punctuation token">.</SPAN>Expression <SPAN class="operator token">=</SPAN> <SPAN class="string token">"$feature.CITY_NAME"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//label using city name</SPAN>
<SPAN class="comment token">//Where clause is set to the OIDs of the selected features</SPAN>
theLabelClass<SPAN class="punctuation token">.</SPAN>WhereClause <SPAN class="operator token">=</SPAN> $<SPAN class="string token">"OBJECTID IN ({string.Join("</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">", selectedOIDs)})"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">//theLabelClass.TextSymbol = Set to any text symbol you want </SPAN>
<SPAN class="comment token">//Set the label definition back to the layer.</SPAN>
lyrOfInterest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">SetDefinition</SPAN><SPAN class="punctuation token">(</SPAN>lyrDefn<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
lyrOfInterest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">SetLabelVisibility</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>