13 Replies Latest reply on Jun 28, 2012 9:11 AM by geospatialweb

    cant add graphic to graphic layer user actionscript

    pvitt
      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
        • Re: cant add graphic to graphic layer user actionscript
          rscheitlin
          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:
          • Re: cant add graphic to graphic layer user actionscript
            pvitt
            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

            .
            • Re: cant add graphic to graphic layer user actionscript
              rscheitlin
              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.
              • Re: cant add graphic to graphic layer user actionscript
                pvitt
                I've attached the component in a text file.
                • Re: cant add graphic to graphic layer user actionscript
                  rscheitlin
                  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]
                  • Re: cant add graphic to graphic layer user actionscript
                    pvitt
                    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
                    • Re: cant add graphic to graphic layer user actionscript
                      rscheitlin
                      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:
                      • Re: cant add graphic to graphic layer user actionscript
                        pvitt
                        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.
                        • Re: cant add graphic to graphic layer user actionscript
                          tomrauch
                          Hi, here's my problem:  I can access all the xml data from a php feed, but the script below returns only one data point.  I should have several hundred.

                          Thanks for any help!  Tom

                          <?xml version="1.0" encoding="utf-8"?>
                          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                xmlns:s="library://ns.adobe.com/flex/spark"
                                xmlns:esri="http://www.esri.com/2008/ags"
                                pageTitle="World Street Map"
                                creationComplete="service.send();" xmlns:mx="library://ns.adobe.com/flex/mx">

                          <!--
                          This sample shows how to add a street basemap layer to your application.

                          The sample uses a cached map service from ArcGIS Online.

                          You can browse the ArcGIS.com site for additional online basemap and reference
                          map services or publish your own geographic data as a service using ArcGIS Server.
                          -->

                          <fx:Declarations>
                           
                           
                            <mx:HTTPService id="service" url="http://192.168.1.49/buoy/flex/metar.php" result="myMap_loadHandler(event)" />
                           
                            <esri:Extent id="initialExtent"
                                xmin="-19325128.942" ymin="-10948057.675" xmax="-539964.871" ymax="18364625.427">
                             <esri:SpatialReference wkid="102100"/>
                            </esri:Extent>
                           
                          </fx:Declarations>


                          <fx:Script>
                            <![CDATA[
                             import com.esri.ags.Graphic;
                             import com.esri.ags.events.ExtentEvent;
                             import com.esri.ags.events.GeometryServiceEvent;
                             import com.esri.ags.events.MapEvent;
                             import com.esri.ags.geometry.Geometry;
                             import com.esri.ags.geometry.MapPoint;
                             import com.esri.ags.geometry.Polygon;
                             import com.esri.ags.symbols.PictureMarkerSymbol;
                             import com.esri.ags.symbols.SimpleMarkerSymbol;
                             import com.esri.ags.tasks.supportClasses.BufferParameters;
                             import com.esri.ags.utils.WebMercatorUtil;
                            
                             import flash.sensors.Geolocation;
                            
                             import mx.collections.ArrayCollection;
                             import mx.collections.XMLListCollection;
                             import mx.rpc.events.ResultEvent;
                            
                             private function myMap_loadHandler(event:ResultEvent):void
                             {
                              var list:ArrayCollection = event.result.markers.marker;
                              for (var i:int=0; i < list.length; i++) {
                              
                               var lat: Number;
                               var lon: Number;
                              
                              
                              
                              
                               var myGraphicMarker:Graphic = new Graphic(new MapPoint(list[i].lat, list[i].lon,
                                new SpatialReference(102100)),
                                new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
                              
                              
                              
                               myGraphicsLayer.add(myGraphicMarker);
                                myGraphicsLayer.refresh();
                              }
                             
                             
                             }
                            
                            ]]>
                          </fx:Script>

                          <esri:Map extent="{initialExtent}" wrapAround180="true" >
                            <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
                            <esri:GraphicsLayer id="myGraphicsLayer"/>
                            
                          </esri:Map>




                          </s:Application>
                          • Re: cant add graphic to graphic layer user actionscript
                            rscheitlin
                            Tom,

                               As this is a new question specific to you and this thread has already been answered, forum etiquette says you should not hijack a thread you should start your own.
                            • Re: cant add graphic to graphic layer user actionscript
                              tomrauch
                              Ah, thanks - will start a new one.
                              • Re: cant add graphic to graphic layer user actionscript
                                geospatialweb
                                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 ran into the same problem with mx.messaging.messages.SOAPMessage and some custom widgets. I'm using FB 4.6 and Flex Viewer 3.0. Problem did not arise wth FB 4.1 and Flex Viewer 2.3.1. Not only did I have to import the class into index.mxml as Robert pointed out but I also had to create a variable of the same class to fix the problem (no error messages in debug console):

                                import mx.messaging.messages.SOAPMessage;

                                private var _dummy:SOAPMessage;

                                John Campbell
                                GIS Application Developer

                                Kingston, Ontario, Canada
                                geospatialweb.ca