Search code examples
javascriptdojoesri

How to get color from a marker symbol using esri and javascript


I use Esri map and javascript, and I set the variable:

var pointSymbol = new esri.symbol.SimpleMarkerSymbol(
    esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE,
    15,
    new esri.symbol.SimpleLineSymbol(
        esri.symbol.SimpleLineSymbol.STYLE_SOLID,
        new dojo.Color([255, 255, 255]),
        2),
    new dojo.Color([47, 71, 122, 0.8]));

And on the graphic click event, I want to get the color of a marker that references this symbol when I click on this marker.


Solution

  • You didn't specify which color you are after, as there might be multiple colors, one for the fill, and one for the outline. But let's assume you want the fill color.

    Let's also assume the feature layer is referenced in the 'fl' object and that the feature layer contains the graphic with the symbol you have mentioned above. Then something like this should work (haven't tested though)

    fl.on('click', function(object){
       var graphic = object.graphic;
       if (!graphic) return;
    
       var symbol = graphic.symbol;
       if (!symbol) return;
    
       var color = symbol.color; // This is the object with a,r,g,b properties
    })
    

    The above also depends on the version of the javascript API, as for the earlier versions the object passed to the event callback used to be directly a graphic object, whereas for the newer versions it's the event object containing the graphic that was clicked. Please consult the esri javascript api documentation here