//
//  ViewController.m
//  MapViewDemo
//

#import "ViewController.h"

@interface ViewController ()<AGSMapViewLayerDelegate>
{
    AGSGraphicsLayer* _gl;
}

@property (weak, nonatomic) IBOutlet AGSMapView *mapView;

@end

@implementation ViewController

- (void)layerDidLoad:(AGSLayer *)layer
{
    if (![layer isKindOfClass:[AGSGraphicsLayer class]])
    {
       [self.mapView removeMapLayer:layer];
    }
    AGSTiledMapServiceLayer* tiledService = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:[NSURL URLWithString:@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"]];
    tiledService.delegate = self;
    [self.mapView insertMapLayer:tiledService atIndex:self.mapView.mapLayers.count - 1];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // set the delegate for the map view
    self.mapView.layerDelegate = self;
    _gl = [AGSGraphicsLayer graphicsLayer];
    _gl.delegate = self;
    [self.mapView addMapLayer:_gl];
}

#pragma mark AGSMapViewLayerDelegate methods

-(void) mapViewDidLoad:(AGSMapView*)mapView
{
    AGSSimpleLineSymbol* symbol = [AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor redColor] width:4];
    AGSEnvelope* e = self.mapView.visibleAreaEnvelope;
    AGSMutablePolyline* p = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference];
    [p addPathToPolyline];
    [p addPointToPath:[AGSPoint pointWithX:e.xmin y:e.ymin spatialReference:self.mapView.spatialReference]];
    [p addPointToPath:[AGSPoint pointWithX:e.xmax y:e.ymax spatialReference:self.mapView.spatialReference]];
    AGSGraphic* g = [AGSGraphic graphicWithGeometry:p symbol:symbol attributes:nil];
    [_gl addGraphic:g];
}

@end