Select to view content in your preferred language

Problem with PictureMarkerSymbol as Graphic

3104
5
Jump to solution
04-09-2012 07:49 AM
BenKane
Regular Contributor
My application has a graphicsLayer with a click handler on the that builds and displays an infoWindow with the graphic's attributes, and changes the symbol of the selected graphic to show that it has been selected.  If I use only SimpleMarkerSymbols as the symbol for the layer the function works fine, but if I use a PictureMarkerSymbol as the symbol the function throws an "Error #1009: Cannot access a property or method of a null object reference" for the graphic var. 

protected function _ClickHandler(clk:MouseEvent):void    {     var graphic:Graphic = clk.target as Graphic;          //reset unselected observationPts to correct symbol     for(var n:Number=0;n<observationPts.graphicProvider.length;n++)     {      observationPts.graphicProvider.symbol = obsPtSym;     }          graphic.symbol = highlightedSym;                                   showInfoWindow();                         }


With the symbols as:
<esri:PictureMarkerSymbol id="obsPtSym"   source="@Embed(source='assets/images/i_pin3_centered.png')"   height="30"   width="30"   />             <esri:SimpleMarkerSymbol id="highlightedSym"             alpha="0.9"            color="0xFF0000"            size="14"            style="circle">           <esri:SimpleLineSymbol color="0x000000"/>     </esri:SimpleMarkerSymbol>


In debug mode I see that clk.target = CustomSprite (@6177581), but the graphic var traces as null.

Anybody dealt with this before?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Ben,

  I got swamped yesterday... Here is a sample app that works with what you are wanting to do.

<?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:mx="library://ns.adobe.com/flex/mx"                xmlns:esri="http://www.esri.com/2008/ags"                initialize="initApp()"                minWidth="955" minHeight="600">     <fx:Declarations>         <esri:PictureMarkerSymbol id="obsPtSym"                                   source="@Embed(source='assets/images/i_pin3_centered.png')"                                   height="30"                                   width="30"                                   />                  <esri:SimpleMarkerSymbol id="highlightedSym"                                   alpha="0.9"                                  color="0xFF0000"                                  size="14"                                  style="circle">             <esri:SimpleLineSymbol color="0x000000"/>         </esri:SimpleMarkerSymbol>     </fx:Declarations>     <fx:Script>         <![CDATA[             import com.esri.ags.Graphic;             import com.esri.ags.geometry.MapPoint;                          protected function _ClickHandler(clk:MouseEvent):void             {                 var graphic:Graphic = clk.currentTarget as Graphic;                                  //reset unselected observationPts to correct symbol                 for(var n:Number=0;n<observationPts.graphicProvider.length;n++)                 {                     observationPts.graphicProvider.symbol = obsPtSym;                 }                                  graphic.symbol = highlightedSym;                                      //showInfoWindow();             }                          private function initApp():void             {                 // add 100 random graphics to the GraphicsLayer                 for (var i:int; i < 100; i++)                 {                     var mapX:Number = Math.random() * 40044000 - 20022000;                     var mapY:Number = Math.random() * 40044000 - 20022000;                                          var attributes:Object = { "ranking": Math.random()};                     var graphic:Graphic = new Graphic(                         new MapPoint(mapX, mapY),                         null,                         attributes                     );                     graphic.toolTip = "Ranking: " + Number(graphic.attributes.ranking).toFixed(3);                     graphic.addEventListener(MouseEvent.CLICK,_ClickHandler);                     graphic.symbol = obsPtSym;                     observationPts.add(graphic);                 }             }         ]]>     </fx:Script>          <esri:Map openHandCursorVisible="false">         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>         <esri:GraphicsLayer id="observationPts" />     </esri:Map> </s:Application>


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

View solution in original post

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Ben,

   Just use this instead as a way to get back to the graphic:

var graphic:Graphic = clk.currentTarget as Graphic;


Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:
0 Kudos
BenKane
Regular Contributor
Robert, while that did change the currentTarget to a graphicsLayer object, the graphic var at graphic.symbol is still null.
0 Kudos
BenKane
Regular Contributor
I see that the problem is that the individual graphic is not being isolated from the graphicsLayer.  Not sure how to isolate the individual graphic clicked or why this would happen when using a PictureMarkerSymbol and not with SimpleMarkerSymbol.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ben,

  I got swamped yesterday... Here is a sample app that works with what you are wanting to do.

<?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:mx="library://ns.adobe.com/flex/mx"                xmlns:esri="http://www.esri.com/2008/ags"                initialize="initApp()"                minWidth="955" minHeight="600">     <fx:Declarations>         <esri:PictureMarkerSymbol id="obsPtSym"                                   source="@Embed(source='assets/images/i_pin3_centered.png')"                                   height="30"                                   width="30"                                   />                  <esri:SimpleMarkerSymbol id="highlightedSym"                                   alpha="0.9"                                  color="0xFF0000"                                  size="14"                                  style="circle">             <esri:SimpleLineSymbol color="0x000000"/>         </esri:SimpleMarkerSymbol>     </fx:Declarations>     <fx:Script>         <![CDATA[             import com.esri.ags.Graphic;             import com.esri.ags.geometry.MapPoint;                          protected function _ClickHandler(clk:MouseEvent):void             {                 var graphic:Graphic = clk.currentTarget as Graphic;                                  //reset unselected observationPts to correct symbol                 for(var n:Number=0;n<observationPts.graphicProvider.length;n++)                 {                     observationPts.graphicProvider.symbol = obsPtSym;                 }                                  graphic.symbol = highlightedSym;                                      //showInfoWindow();             }                          private function initApp():void             {                 // add 100 random graphics to the GraphicsLayer                 for (var i:int; i < 100; i++)                 {                     var mapX:Number = Math.random() * 40044000 - 20022000;                     var mapY:Number = Math.random() * 40044000 - 20022000;                                          var attributes:Object = { "ranking": Math.random()};                     var graphic:Graphic = new Graphic(                         new MapPoint(mapX, mapY),                         null,                         attributes                     );                     graphic.toolTip = "Ranking: " + Number(graphic.attributes.ranking).toFixed(3);                     graphic.addEventListener(MouseEvent.CLICK,_ClickHandler);                     graphic.symbol = obsPtSym;                     observationPts.add(graphic);                 }             }         ]]>     </fx:Script>          <esri:Map openHandCursorVisible="false">         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>         <esri:GraphicsLayer id="observationPts" />     </esri:Map> </s:Application>


Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:
0 Kudos
BenKane
Regular Contributor
Robert, thank you.

It turns out my problem was that I had attached the _ClickHandler to the graphicsLayer, rather than to each individual graphic.  When I look through and add it as an event listener to each graphic it works fine.
0 Kudos