Add a graphicsLayers

642
2
09-15-2011 05:45 PM
ShaunWeston
Occasional Contributor
This is probably a simple answer, but for some reason I just can't get a graphics layer to add when I click on a map. THe alert "map clicked" does come up, but the point graphic layer is not added to the map?

   private var myGraphicsLayer:GraphicsLayer = new GraphicsLayer();
   
   private var configFeatureLayers:Array;
   
   //this function called when the widget's configuration is loaded
   private function basewidget_widgetConfigLoadedHandler(event:Event):void
   {
    if (map.loaded)
    {
     Alert.show("map loaded");
     loadHandler(null);
    }
    else
    {      
     map.addEventListener(MapEvent.LOAD, loadHandler);
    } 
    function loadHandler(event:MapEvent):void
    {
     map.removeEventListener(MapEvent.LOAD, loadHandler);
     map.addEventListener(MapMouseEvent.MAP_CLICK, clickHandler);
    }
    function clickHandler(event:MapMouseEvent):void 
    {
     showPopup(event.mapPoint);
    }
    
    function showPopup(mapPoint:MapPoint):void
    { 
     Alert.show("map clicked"); 
     myGraphicsLayer.clear();
     var graphic:Graphic = new Graphic(mapPoint, clickPtSym);
     myGraphicsLayer.add(graphic); 
Tags (2)
0 Kudos
2 Replies
IvanBespalov
Regular Contributor
<viewer:BaseWidget ...>
...
private var myGraphicsLayer:GraphicsLayer;
  
/**
* Listen widget template open handler
*/
protected function openHandler(event:Event):void
{
    myGraphicsLayer = getMyGraphicsLayer();
    if (!myGraphicsLayer)
    {
        // show alert 'Graphics Layer is null'
trace("GraphicsLayer not found");
    }
    else
    {
        trace(StringUtil.substitute("GraphicsLayer with id={0} found", myGraphicsLayer.id));
    }
}

/**
* Get existing graphics layer or add new if does not exists
*/  
private function getMyGraphicsLayer():GraphicsLayer
{
    var result:GraphicsLayer = null;
    if (map)
    {
        var mapLayers:ArrayCollection = map.layers as ArrayCollection;
        for each (var mapLayer:Layer in mapLayers)
{
     if (mapLayer is GraphicsLayer)
     {
         result = mapLayer as GraphicsLayer;
  map.addEventListener(MapMouseEvent.MAP_MOUSE_DOWN, onMapClick);
  break;
     }
}

        // if map has not any graphics layer add new   
if (!result)
{
     result = new GraphicsLayer();
     map.addLayer(result);
}
    }
 
    return result;
}
  
/**
* Listen map mouse down event handler
*/
protected function onMapClick(event:MapMouseEvent):void
{
    if (myGraphicsLayer)
    {
        var grGeometry:MapPoint = event.mapPoint;
var sms:SimpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, 0xFF0000);
        var attributes:Object = new Object();
attributes.dateadded = new Date();
var gr:Graphic = new Graphic(grGeometry, sms, attributes);
    
var grId:String = myGraphicsLayer.add(gr);
trace(StringUtil.substitute("Graphic with id: {0} added.", grId));
    }
}
  
/**
* Listen widget template close handler
*/
private function closedHandler(event:Event):void
{
    if (map)
    {
        map.removeEventListener(MapMouseEvent.MAP_MOUSE_DOWN, onMapClick);
    }
}
....
<viewer:WidgetTemplate id="wTemplate"
      width="300"
      height="300"
       closed="closedHandler(event)"
      open="openHandler(event)">
<!-- Here are widget contents -->
</viewer:WidgetTemplate>
</viewer:BaseWidget>
0 Kudos
ShaunWeston
Occasional Contributor
Oh yeah thanks,

I managed to fix it and get it working, I'm not sure exactly what the problem was.
0 Kudos