multicolored graphics/sketch layer

1005
7
08-31-2012 07:06 AM
HamzaHaroon
Occasional Contributor
I'm trying to add different colored graphics to my graphics layer. I have made a graphics renderer and set the AGSSimpleFillSymbol.

When i change the color of the fill symbol, the graphics layer redraws the previous graphics with the new color. How do i make the graphics layer hold graphics of different colors?
0 Kudos
7 Replies
NimeshJarecha
Esri Regular Contributor
You should assign symbol to each graphic rather than assigning a renderer to the graphics layer.

Regards,
Nimesh
0 Kudos
HamzaHaroon
Occasional Contributor
this is how i'm adding graphics to my graphics layer

AGSGeometry* sketchGeometry = [[self.sketchLayer.geometry copy] autorelease];
AGSGraphic* graphic = [AGSGraphic graphicWithGeometru: sketchGeometry symbol: lineColor attributes: nil infoTemplateDelegate: nil];
[self.graphicsLayer addGraphic:graphic];

my lineColor is an AGSFillSymbol and assigned in the viewDidLoad method.

AGSCompositeSymbol *mainSym = self.gpsSketchLayer.mainSymbol;
        for (AGSSymbol *s in mainSym.symbols) {
            if ([s isKindOfClass:[AGSSimpleFillSymbol class]])
            {
                lineColor = (AGSSimpleFillSymbol *)s;
                lineColor.outline.color = [UIColor blueColor];   
            }
        }

I change my lineColor from buttons.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Did you remove renderer from graphics layer?

Regards,
Nimesh
0 Kudos
HamzaHaroon
Occasional Contributor
yea. still changes all of my graphics to the same color.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Are you setting different color for the symbol each time before assigning it to the graphic?
Post a sample application so I can have a look.

Regards,
Nimesh
0 Kudos
HamzaHaroon
Occasional Contributor
heres my code in my viewController class. implementing the graphics layer here.
#import "GMViewController.h"

@interface GMViewController ()

@end

@implementation GMViewController
@synthesize painting;
@synthesize colorPalette;
@synthesize colorPicker;
@synthesize mapView = _mapView;
@synthesize tiledLayer;
@synthesize dynamicLayer;
@synthesize sketchLayer;
@synthesize graphicsLayer;
@synthesize midSymbol;
@synthesize vertSymbol;
@synthesize cred;
@synthesize toolbar = _toolbar;
@synthesize sketchToolbar = _sketchToolbar;
@synthesize lineColor;
@synthesize gLine;

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    painting1 = [[PaintingView alloc] init];
    [painting1 setDelegate:self];
    
    [painting setDelegate:self];
    
    NSString *backgroundLayer = kTiledMapServiceURL1;
    
    tiledLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:backgroundLayer]];
    
    [self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];
    
    NSString *pass = @"mobile1!";
    cred = [[AGSCredential alloc] initWithUser:@"mobile" password:pass];
    
    dynamicLayer = [[AGSDynamicMapServiceLayer alloc] initWithURL:[NSURL URLWithString: kDynamicMapServiceURL] credential:cred];
    
    [self.mapView addMapLayer:dynamicLayer withName:@"Dynamic Layer"];
    
    
    //Graphics layer to hold all sketches (points, polylines, and polygons)
  graphicsLayer = [AGSGraphicsLayer graphicsLayer];
 [self.mapView addMapLayer:graphicsLayer withName:@"Graphics Layer"];
    
    AGSGraphicsLayer *graphicsLayer2 = [AGSGraphicsLayer graphicsLayer];
    [self.mapView addMapLayer:graphicsLayer2 withName:@"Graphics Layer 2"];
    
    
 //A composite symbol for the graphics layer's renderer to symbolize the sketches
 AGSCompositeSymbol* composite = [AGSCompositeSymbol compositeSymbol];
 AGSSimpleMarkerSymbol* markerSymbol = [[[AGSSimpleMarkerSymbol alloc] init] autorelease];
 //markerSymbol.style = AGSSimpleMarkerSymbolStyleSquare;
 //markerSymbol.color = [UIColor greenColor];
 //[composite.symbols addObject:markerSymbol];
 AGSSimpleLineSymbol* lineSymbol = [[[AGSSimpleLineSymbol alloc] init] autorelease];
 lineSymbol.color= [UIColor redColor];
 lineSymbol.width = 4;
 [composite.symbols addObject:lineSymbol];
 AGSSimpleFillSymbol* fillSymbol = [[[AGSSimpleFillSymbol alloc] init] autorelease];
 fillSymbol.outline.color = [UIColor redColor];//colorWithRed:1.0 green:0 blue:0 alpha:4] ;
    fillSymbol.outline.width = 4;
 [composite.symbols addObject:fillSymbol];
 AGSSimpleRenderer* renderer = [AGSSimpleRenderer simpleRendererWithSymbol:composite];
 //graphicsLayer.renderer = renderer;
    graphicsLayer2.renderer = renderer;
    
 //Sketch layer
 sketchLayer = [[[AGSSketchGraphicsLayer alloc] initWithGeometry:nil] autorelease];
 [self.mapView addMapLayer:sketchLayer withName:@"Sketch layer"];
    
    AGSSketchGraphicsLayer *sketchLayer2 = [[[AGSSketchGraphicsLayer alloc] initWithGeometry:nil]autorelease];
    [self.mapView addMapLayer:sketchLayer2 withName:@"Sketch layer2"];
 
 //Helper class to manage the UI toolbar, Sketch Layer, and Graphics Layer
 //Basically, where the magic happens
 self.sketchToolbar = [[[GMSketchToolBar alloc] initWithToolbar:self.toolbar
                                                     sketchLayer:sketchLayer2
                                                         mapView:self.mapView
                                                   graphicsLayer:graphicsLayer2] autorelease];
    
    poly = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference];
    midSymbol = self.sketchLayer.midVertexSymbol;
    vertSymbol = self.sketchLayer.vertexSymbol;
    self.sketchLayer.midVertexSymbol = nil;
    self.sketchLayer.vertexSymbol = nil;
    
    AGSSpatialReference *sr = [AGSSpatialReference spatialReferenceWithWKID:1984];
    AGSEnvelope *env = [AGSEnvelope envelopeWithXmin:-16.7178 ymin:10.9251 xmax:-13.6439 ymax:12.6847 spatialReference:sr];
    
    [self.mapView zoomToEnvelope:env animated:YES];
    
    gLine.outline.color = [UIColor redColor];
    
    AGSSimpleFillSymbol *gfxLine = [[[AGSSimpleFillSymbol alloc] init] autorelease];
    gfxLine.outline.color = [UIColor redColor];
    //gLine = (AGSFillSymbol* )gfxLine;
    
    
    AGSCompositeSymbol *mainSym = self.sketchLayer.mainSymbol;
    for (AGSSymbol *s in mainSym.symbols)
    {
        if ([s isKindOfClass:[AGSSimpleFillSymbol class]])
        {
            lineColor = (AGSSimpleFillSymbol *)s;
            lineColor.outline.color = [UIColor redColor]; //changes line
            lineColor.outline.width = 4; // changes width of line.
        }
    }
    
}

-(void)makeLine:(CGPoint)touch firstTouch:(BOOL)firstTouch lineFinished:(BOOL)lineFinished
{
    if (firstTouch)
    {
        //poly = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference];

        //self.sketchLayer.geometry = [[[AGSMutablePolyline alloc]initWithSpatialReference:self.mapView.spatialReference]autorelease];
        [poly addPathToPolyline];
        //polly = [[[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference]autorelease];
    }

    [poly addPointToPath:[self.mapView toMapPoint:touch]];
    
    self.sketchLayer.geometry = poly;
}

-(void) mapView:(AGSMapView *) mapView failedLoadingLayerForLayerView:(UIView<AGSLayerView> *)layerView baseLayer:(BOOL)baseLayer withError:(NSError *)error
{
    if (!baseLayer)
        [self.mapView removeMapLayerWithName:layerView.name];
}

- (void)viewDidUnload
{
    [self setMapView:nil];
    [self setPainting:nil];
    [self setColorPicker:nil];
    [self setColorPalette:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void)dealloc
{
    [painting1 release];
    self.mapView = nil;
    self.toolbar = nil;
    self.sketchToolbar = nil;
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)pullPaintBoard:(id)sender {
    CGRect redLiningFrame = painting.frame;
    CGRect colorFrame = colorPalette.frame;

    if (redLiningFrame.origin.x == 0)
    {
        redLiningFrame.origin.x = 1024;
        colorFrame.origin.x = 1024;

        AGSGeometry* sketchGeometry = [[self.sketchLayer.geometry copy] autorelease];
        //gLine.outline.color = lineColor.outline.color;
        AGSGraphic* graphic = [AGSGraphic graphicWithGeometry:sketchGeometry symbol:lineColor attributes:nil infoTemplateDelegate:nil];
  [self.graphicsLayer addGraphic:graphic];
        
        
        [self.graphicsLayer dataChanged];
        [self.sketchLayer clear];
        
    }
    else
    {
        redLiningFrame.origin.x = 0;
        colorFrame.origin.x = 60;
    }
    
    painting.frame = redLiningFrame;
    colorPalette.frame = colorFrame;
}
- (IBAction)colorPicker:(id)sender {
    
}
- (IBAction)selectRed:(id)sender {
    lineColor.outline.color = [UIColor redColor];
    gLine.outline.color = [UIColor redColor];
}

- (IBAction)selectBlue:(id)sender {
    lineColor.outline.color = [UIColor blueColor];
    gLine.outline.color = [UIColor blueColor];
}

- (IBAction)clearScreen:(id)sender {
    [self.graphicsLayer removeAllGraphics];
    [self.graphicsLayer dataChanged];
}
@end


edited code slightly to add lines to graphic layer.

lines are created in my makeLine method. they are added to the graphic layer in the pullPaintBoard method.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Please attach an application which I can run. It's very hard to read the code and help you.

Regards,
Nimesh
0 Kudos