Select to view content in your preferred language

Display point with action script fails in widget

941
5
08-29-2013 05:57 AM
MikeSmith7
Emerging Contributor
I have created a widget and I can not get it to put a SimpleMarkerSymbol at the location (x,y).  The code compiles but myGraphicsLayer.add(myGraphicMarker); is not firing.  I clearly am missing something.

Thanks,
Mike
Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Mike,


   A new MapPoint is expecting an x (Number) and a Y (Number) NOT a MapPoint object which is what featureSet.features[0].geometry is returning. Also featureSet.features[0] is a graphic already so why not just add it to the Graphics layer?

var myGraphicMarker:Graphic = new Graphic(new MapPoint(featureSet.features[0].geometry),    
                new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
0 Kudos
MikeSmith7
Emerging Contributor
I had that very line in as part of my testing, I even hard coded a X and Y value and could not get it to shop up. There is something simple I am missing, but have been banging my head for a while now on this on.

Thanks,
Mike



Code:
var myGraphicMarker:Graphic = new Graphic(new MapPoint(featureSet.features[0].geometry),   
   

<?xml version="1.0" encoding="utf-8"?>
<!--
///////////////////////////////////////////////////////////////////////////
// Written by Micheal W. Smith
//
// Aug 15, 2013
// This Widget is meant to read the URL, parse for a specific parameter (facility or premise) and a unique id
// The result is zoomed, centered and marked with a point.
//
///////////////////////////////////////////////////////////////////////////
-->
<viewer:BaseWidget 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:Search="widgets.Search.*"
       xmlns:viewer="com.esri.viewer.*"
       xmlns:esri="http://www.esri.com/2008/ags"
       xmlns:components="com.esri.viewer.components.*"
       x="600" y="300"
       initialize="basewidget_initializeHandler()"
       widgetConfigLoaded="basewidget_widgetConfigLoaded()">

<viewer:states>
  <s:State name="graphicalInput"/>
  <s:State name="textInput"/>
  <s:State name="resultsList"/>
</viewer:states>

<viewer:transitions>
  <s:Transition autoReverse="true" toState="*">
   <s:Fade id="fade"/>
  </s:Transition>
</viewer:transitions>

<fx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.layers.GraphicsLayer;
   import com.esri.ags.symbols.PictureMarkerSymbol;
   import com.esri.ags.symbols.Symbol;
  
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   import mx.rpc.AsyncResponder;
  
   [Bindable]
   public var UniqueID:String = "000000";
   private var zoomScale:Number = 10000;
   private var graphicsLayer:GraphicsLayer;
   private var resultSymbol:Symbol;

  
   private function basewidget_initializeHandler():void
   {
   
   }
  
   private function basewidget_widgetConfigLoaded():void
   {
    var layer:String =""

    if (ExternalInterface.available)
    {
 
     // Use JavaScript to get the search string from the current browser location.
     // See http://livedocs.adobe.com/flex/3/langref/flash/external/ExternalInterface.html
     var urlstring:String = ExternalInterface.call("function(){ return document.location.href.toString();}");
     var words:Array;
     if (urlstring && urlstring.length > 0)
     {
      if (urlstring.indexOf("?premise=") > 0)
      {
       layer="premise";
       words=urlstring.split("?premise=");
       UniqueID=words[1];
      } 
      else if (urlstring.indexOf("?facility=") > 0)
      {
       layer="facility";
       words=urlstring.split("?facility=");
       UniqueID=words[1];
      } 
     
      if (layer=="premise")
      {
       queryPremise.execute(queryPoint, new AsyncResponder(onResult, onFault));
//       Alert.show("Searching Premise");
      }
      else if (layer=="facility")
      {
       queryFacility.execute(queryPoint, new AsyncResponder(onResult, onFault));
//       Alert.show("Searching Facility");
      }
     
     }
      function onResult(featureSet:FeatureSet, token:Object = null):void
      {
       if (featureSet.features.length == 0)
       {
        Alert.show("No mapable items found. Please try again.");
       }
       else
       {
        map.scale = zoomScale;
        map.centerAt(featureSet.features[0].geometry as MapPoint);
        import com.esri.ags.SpatialReference;
//        import com.esri.ags.symbols.PictureMarkerSymbol;
       
        
        //var myGraphicMarker:Graphic = new Graphic(new MapPoint(featureSet.features[0].geometry),new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
//        var myGraphicMarker:Graphic = new Graphic(new MapPoint(-7033223.1, 5827260.9, new SpatialReference(102100)),
//         new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
       
        var myGraphicMarker:Graphic = new Graphic(new MapPoint(featureSet.features[0].geometry),   
         new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
       
        //var myGraphicMarker:Graphic = new Graphic(new MapPoint(featureSet.features[0].geometry),
//         new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 22, 0x009933));
        //myGraphicMarker.toolTip = "Marker added with ActionScript";
        //var myGraphicsLayer:GraphicsLayer
        Alert.show("A");
       
       
//       graphicsLayer.add(myGraphicMarker);
      
          //adding the graphics layer to the map
   
        //map.addLayer(myGraphicsLayer);    
       
       
         Alert.show("B");
//         myGraphicsLayer.add(myGraphicMarker);
       }
      }
      function onFault(info:Object, token:Object = null):void
      {
       Alert.show(info.toString());
      }      
     }

    else
    {
     Alert.show("interface not accessable");
    }

   }
  

  ]]>
</fx:Script>

<fx:Declarations>
  <!-- Symbol for Query Result as Point -->
  <esri:SimpleMarkerSymbol id="sfs" alpha="36" color="Black"/>
 
  <!-- Layer with Premises -->
  <esri:QueryTask id="queryPremise" url="http://atlantis:6080/arcgis/rest/services/SearchTest/MapServer/0"/>
  <esri:QueryTask id="queryFacility" url="http://atlantis:6080/arcgis/rest/services/SearchTest/MapServer/3"/>
 
  <esri:Query id="queryPoint"
     outSpatialReference="{map.spatialReference}"
     returnGeometry="true"
     text="{UniqueID}">
  </esri:Query>
</fx:Declarations>




</viewer:BaseWidget>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Mike,

   You dis-took the code that I posted as code I wanted you to use when, In the text of my post I tell you that that code is incorrect. I just posted the BAD code to show you where your mistake was.

Here is code that should work:
                    function onResult(featureSet:FeatureSet, token:Object = null):void
                    {
                        if (featureSet.features.length == 0)
                        {
                            Alert.show("No mapable items found. Please try again.");
                        }
                        else
                        {
                            map.scale = zoomScale;
                            map.centerAt(featureSet.features[0].geometry as MapPoint);
                            
                            var myGraphicMarker:Graphic = featureSet.features[0] as Graphic;
                            myGraphicMarker.symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933);
                            
                            var myGraphicsLayer:GraphicsLayer = new GraphicsLayer();
                            graphicsLayer.add(myGraphicMarker);
                            
                            //adding the graphics layer to the map
                            map.addLayer(myGraphicsLayer);
                        }
                    }
0 Kudos
MikeSmith7
Emerging Contributor
Sorry Robert, I misunderstood.  I added the code and I get the same result (lack of a marker).  I put a try and catch around
try
{
    graphicsLayer.add(myGraphicMarker);
}
catch
{
    Alert.show(error.toString());
}

I get  TypeError: Error #1009
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Mike,

   OK Here is a working version of your widget:

<?xml version="1.0" encoding="utf-8"?>
<!--
///////////////////////////////////////////////////////////////////////////
// Written by Micheal W. Smith
//
// Aug 15, 2013
// This Widget is meant to read the URL, parse for a specific parameter (facility or premise) and a unique id
// The result is zoomed, centered and marked with a point.
//
///////////////////////////////////////////////////////////////////////////
-->
<viewer:BaseWidget 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:Search="widgets.Search.*"
                   xmlns:viewer="com.esri.viewer.*"
                   xmlns:esri="http://www.esri.com/2008/ags"
                   xmlns:components="com.esri.viewer.components.*"
                   creationComplete="basewidget_widgetConfigLoaded()">
    
    <fx:Script>
        <![CDATA[
            import com.esri.ags.FeatureSet;
            import com.esri.ags.Graphic;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.layers.GraphicsLayer;
            import com.esri.viewer.ViewerContainer;
            
            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
            
            [Bindable]
            public var UniqueID:String = "000000";
            private var zoomScale:Number = 10000;
            private var graphicsLayer:GraphicsLayer;

            
            private function basewidget_widgetConfigLoaded():void
            {
                var layer:String ="";
                
                if(ViewerContainer.urlConfigParams.premise != null){
                    layer="premise";
                    UniqueID = ViewerContainer.urlConfigParams.premise;
                    queryPremise.execute(queryPoint, new AsyncResponder(onResult, onFault));
                }
                if(ViewerContainer.urlConfigParams.facility != null){
                    layer="facility";
                    UniqueID = ViewerContainer.urlConfigParams.ViewerContainer.urlConfigParams.facility;
                    queryFacility.execute(queryPoint, new AsyncResponder(onResult, onFault));
                }
                
                function onResult(featureSet:FeatureSet, token:Object = null):void
                {
                    if (featureSet.features.length == 0)
                    {
                        Alert.show("No mapable items found. Please try again.");
                    }
                    else
                    {
                        map.scale = zoomScale;
                        map.centerAt(featureSet.features[0].geometry as MapPoint);
                        
                        var myGraphicMarker:Graphic = featureSet.features[0] as Graphic;
                        myGraphicMarker.symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933);
                        
                        graphicsLayer = new GraphicsLayer();
                        graphicsLayer.add(myGraphicMarker);
                        
                        //adding the graphics layer to the map
                        map.addLayer(graphicsLayer);
                    }
                }
                
                function onFault(info:Object, token:Object = null):void
                {
                    Alert.show(info.toString());
                }
            }

        ]]>
    </fx:Script>
    
    <fx:Declarations>
        <!-- Symbol for Query Result as Point -->
        <esri:SimpleMarkerSymbol id="sfs" alpha="36" color="Black"/>
        
        <!-- Layer with Premises -->
        <esri:QueryTask id="queryPremise" url="http://atlantis:6080/arcgis/rest/services/SearchTest/MapServer/0"/>
          <esri:QueryTask id="queryFacility" url="http://atlantis:6080/arcgis/rest/services/SearchTest/MapServer/3"/>
        
        <esri:Query id="queryPoint"
                    outSpatialReference="{map.spatialReference}"
                    returnGeometry="true"
                    text="{UniqueID}">
        </esri:Query>
    </fx:Declarations>
</viewer:BaseWidget> 
0 Kudos