cant add graphic to graphic layer user actionscript

2883
13
Jump to solution
03-05-2012 01:16 PM
PeteVitt
Occasional Contributor III
I just cant seem to add a graphic to my graphic layer using actionscript --
I'm trying to add a point graphic where the user clicks the map -- this is what I have:

protected function MapClickHandler(event:MapMouseEvent):void
{
  const mapPoint:MapPoint = theMap.toMapFromStage(event.stageX, event.stageY);
  mapPoint.spatialReference = new SpatialReference(102100);
       
  var myGraphicMarker:Graphic = new Graphic(mapPoint,
  new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
    
  myGraphicMarker.toolTip = "Marker added with ActionScript";
  pointGraphicsLayer.add(myGraphicMarker);
  pointGraphicsLayer.refresh();
   
}

<esri:Map id="theMap"
   mapClick="MapClickHandler(event)">
<esri:GraphicsLayer id="pointGraphicsLayer">
</esri:Map>

I've confirmed that event.stageX and stageY are returning map coords by putting the results in a text box -- Any help on what I could be missing would be appreciated.

Thanks

Pete
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Pete,

   So what that message means is that you need to import
import com.esri.ags.geometry.MapPoint;
in your main mxml Application.

You still need to mark this post as answered (follow the pictures steps left to right).

Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:

View solution in original post

0 Kudos
13 Replies
RobertScheitlin__GISP
MVP Emeritus
Pete,

   Try using the built-in MapPoint that is returned by MapMouseEvent.

            protected function MapClickHandler(event:MapMouseEvent):void
            {
                const mapPoint:MapPoint = event.mapPoint;
                mapPoint.spatialReference = new SpatialReference(102100);
                
                var myGraphicMarker:Graphic = new Graphic(mapPoint,
                    new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
                
                myGraphicMarker.toolTip = "Marker added with ActionScript";
                pointGraphicsLayer.add(myGraphicMarker);
                pointGraphicsLayer.refresh();
                
            }


And you need to terminate your GraphicsLayer:

<esri:Map id="theMap" mapClick="MapClickHandler(event)">
<esri:GraphicsLayer id="pointGraphicsLayer" />
</esri:Map>


Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
EmilyParson
New Contributor
It is useful for me!
0 Kudos
PeteVitt
Occasional Contributor III
Thanks Robert - I tried that method but the graphics still dont appear.   The graphicAdd event on the graphicsLayer does fire after the code is run, however so the code is adding the graphic to the graphic layer, but it doesnt appear.  I also tried changing the wkid to 3857 -- that didnt work either

Pete

.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Pete,

   You need to share more of your code than because I did a quick and simple app using what I posted and it works fine.
0 Kudos
PeteVitt
Occasional Contributor III
I've attached the component in a text file.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Pete,

   Your issue is not in the code you sent me because I took it and adde the CompProjectLocation.mxml and added it to an new Application and commented out the skins I did not get from you and the Nav tools and When I click on the map a blue diamond appears...
[ATTACH=CONFIG]12452[/ATTACH]
0 Kudos
PeteVitt
Occasional Contributor III
Thanks Robert for looking at this.  I've had warnings pop up in my console about some of the esri classes I'm importing.  Warnings such as:
The class com.esri.ags.geometry.MapPoint has been used in a call to net.registerClassAlias() in _ModRound2Form_FlexInit. This will cause Round2Form:ModRound2Form to be leaked. To resolve the leak, define com.esri.ags.geometry.MapPoint in the top-level application.  

I need to dig more into this I guess.  The map component is being used in ModRound2Form -- ModRound2Form is one of a number of modules that are swapped out using ModuleLoader in the main application
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Pete,

   So what that message means is that you need to import
import com.esri.ags.geometry.MapPoint;
in your main mxml Application.

You still need to mark this post as answered (follow the pictures steps left to right).

Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
PeteVitt
Occasional Contributor III
I had the map component inside of an mx:tabNavigator and that seemed (for whatever reason) to be the cause of the graphic not appearing on the map when clicked.  When I put the component inside of a viewstack it worked fine.
0 Kudos