Tuesday, September 9, 2008

Custom Symbol for Graphic using Flex API for ArcGIS

This is demonstration of a very simple custom symbol to render a graphic component on a map. Here is the application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:esri="http://www.esri.com/2008/ags"
xmlns:test="com.esri.test.*"
layout="absolute"
>
<esri:Map>
<esri:GraphicsLayer>
<esri:graphicProvider>
<mx:ArrayCollection>
<esri:Graphic>
<esri:geometry>
<esri:MapPoint x="45" y="45"/>
</esri:geometry>
<esri:symbol>
<test:CustomSymbol/>
</esri:symbol>
</esri:Graphic>
</mx:ArrayCollection>
</esri:graphicProvider>
</esri:GraphicsLayer>
</esri:Map>
</mx:Application>

And here is the CustomSymbol:

package com.esri.test
{
import com.esri.ags.Graphic;
import com.esri.ags.Map;
import com.esri.ags.esri_internal;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.symbol.Symbol;

use namespace esri_internal;

public class CustomSymbol extends Symbol
{
public function CustomSymbol()
{
super();
}

override esri_internal function drawGraphic(
map : Map,
graphic : Graphic
) : void
{
if( graphic.geometry is MapPoint )
{
drawMapPoint( map, graphic, MapPoint( graphic.geometry ));
}
}

private function drawMapPoint(
map : Map,
graphic : Graphic,
mapPoint : MapPoint
) : void
{
graphic.x = map.mapToContainerX(mapPoint.x);
graphic.y = map.mapToContainerY(mapPoint.y);

graphic.graphics.clear();
graphic.graphics.beginFill( 0xFF0000, 0.5 );
graphic.graphics.drawCircle( 0, 0, 10 );
graphic.graphics.endFill();
}

}
}

Be warned - this uses an esri_internal function that is subject to change without notice :-(

6 comments:

Prem said...

How is this different from using the inforenderer property of a graphic to assign a new infosymbol to it?

thunderhead said...

Sorry for the delay - there is no difference - the display of graphic is invalidated upon a new symbol or is a property of s symbol (infoRenderer in this case) changes :-)

Prem said...

ok Thanks thats great to know I was just trying to make sure that theres not one right way or better way to do it.

Unknown said...

Hi,

I'm getting "Method marked override must override another method" on "override esri_internal"I'm sure I'm missing something.

Unknown said...
This comment has been removed by the author.
thunderhead said...

If you are using the new 1.1 version - use the toScreenX(map, mapX) and toScreenY(map, mapY) :-)