Code Listings
ClassBreaksViewController.h
01 | #import <UIKit/UIKit.h> |
07 | @interface ClassBreaksViewController : UIViewController<AGSMapViewDelegate, AGSQueryTaskDelegate> { |
09 | AGSGraphicsLayer *cityGraphicsLayer; |
10 | AGSQueryTask *cityQueryTask; |
14 | @property (nonatomic, retain) IBOutlet AGSMapView *mapView; |
15 | @property (nonatomic, retain) AGSGraphicsLayer *cityGraphicsLayer; |
16 | @property (nonatomic, retain) AGSQueryTask *cityQueryTask; |
ClassBreaksViewController.m
001 | #import "ClassBreaksViewController.h" |
003 | @implementation ClassBreaksViewController |
006 | @synthesize cityGraphicsLayer; |
007 | @synthesize cityQueryTask; |
009 | // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. |
013 | // Set map view delegate |
014 | self.mapView.mapViewDelegate = self; |
016 | // Create tile base map layer |
017 | AGSTiledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc] initWithURL:[NSURL URLWithString:kTiledMapServiceURL]]; |
018 | [self.mapView addMapLayer:tiledLayer withName:@"BaseLayer"]; |
019 | [tiledLayer release]; |
021 | // Create grpahics layer |
022 | self.cityGraphicsLayer = [AGSGraphicsLayer graphicsLayer]; |
024 | // Create symbols for the three class breaks |
025 | AGSSimpleMarkerSymbol *lowSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol]; |
026 | lowSymbol.color = [UIColor colorWithRed:151.0/255.0 green:216.0/255.0 blue:255.0/255.0 alpha:0.8]; |
027 | lowSymbol.outline.width = 0; |
030 | AGSSimpleMarkerSymbol *mediumSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol]; |
031 | mediumSymbol.color = [UIColor colorWithRed:255.0/255.0 green:165.0/255.0 blue:83.0/255.0 alpha:0.8]; |
032 | mediumSymbol.outline.width = 0; |
033 | mediumSymbol.size = 20; |
035 | AGSSimpleMarkerSymbol *highSymbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol]; |
036 | highSymbol.color = [UIColor colorWithRed:222.0/255.0 green:0.0 blue:0.0 alpha:0.8]; |
037 | highSymbol.outline.width = 0; |
038 | highSymbol.size = 25; |
040 | // Create a class breaks renderer with a default simple marker symbol and an attribute field |
041 | AGSClassBreaksRenderer *cityRenderer = [AGSClassBreaksRenderer |
042 | classBreaksRendererWithDefaultSymbol:lowSymbol |
043 | forAttributeField:@"POP1990"]; |
045 | // Create three AGSClassBreak objects, one each for low, medium and high populations |
046 | AGSClassBreak* lowClassBreak = [AGSClassBreak |
047 | classBreakInfoWithSymbol:lowSymbol forMinValue:DBL_MIN |
050 | AGSClassBreak* mediumClassBreak = [AGSClassBreak |
051 | classBreakInfoWithSymbol:mediumSymbol forMinValue:50000.0 |
054 | AGSClassBreak* highClassBreak = [AGSClassBreak |
055 | classBreakInfoWithSymbol:highSymbol forMinValue:250000.0 |
058 | // Create an NSMutableArray, fill it with the class break objects, |
059 | // and set it to the renderer’s classBreaks property |
060 | cityRenderer.classBreaks = [NSMutableArray arrayWithObjects: |
061 | lowClassBreak, mediumClassBreak, highClassBreak, nil]; |
063 | // Add the renderer to the graphics layer |
064 | self.cityGraphicsLayer.renderer = cityRenderer; |
066 | // Add the graphics layer to the map view |
067 | [self.mapView addMapLayer:self.cityGraphicsLayer withName:@"CityGraphicsLayer"]; |
070 | // Override to allow orientations other than the default portrait orientation. |
071 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
072 | // Return YES for supported orientations |
076 | - (void)didReceiveMemoryWarning { |
077 | // Releases the view if it doesn't have a superview. |
078 | [super didReceiveMemoryWarning]; |
080 | // Release any cached data, images, etc that aren't in use. |
083 | - (void)viewDidUnload { |
084 | // Release any retained subviews of the main view. |
085 | // e.g. self.myOutlet = nil; |
086 | self.cityGraphicsLayer = nil; |
088 | [super viewDidUnload]; |
092 | [cityGraphicsLayer release]; |
097 | #pragma mark AGSMapViewDelegate |
099 | // Called when the map view is loaded (after the view is loaded) |
100 | - (void)mapViewDidLoad:(AGSMapView *)mapView { |
102 | // Set up query task for cities and perform query returning all attributes |
103 | self.cityQueryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:kDynamicMapServiceURL]]; |
104 | self.cityQueryTask.delegate = self; |
106 | AGSQuery *cityQuery = [AGSQuery query]; |
107 | cityQuery.where = @"STATE_NAME = 'California'"; |
108 | cityQuery.outFields = [NSArray arrayWithObject:@"*"]; |
110 | [self.cityQueryTask executeWithQuery:cityQuery]; |
112 | // Create extent to be used as default |
113 | AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-118.6 |
117 | spatialReference:self.mapView.spatialReference]; |
119 | // Call method to set extent, pass in envelope |
120 | [self.mapView performSelector:@selector(zoomToEnvelope:animated:) |
125 | #pragma mark AGSQueryTaskDelegate |
127 | // When query is executed .... |
128 | - (void)queryTask:(AGSQueryTask *)queryTask didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet { |
129 | // Iterate the returned features (graphics) and add them to the graphics layer |
130 | for (AGSGraphic *graphic in featureSet.features) { |
131 | [self.cityGraphicsLayer addGraphic:graphic]; |
133 | [self.cityGraphicsLayer dataChanged]; |
135 | // Clean up query task memory |
136 | self.cityQueryTask = nil; |
139 | // If there's an error with the query task give info to user |
140 | - (void)queryTask:(AGSQueryTask *)queryTask didFailWithError:(NSError *)error { |
141 | // Clean up query task memory |
142 | self.cityQueryTask = nil; |
144 | // Display error message |
145 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" |
146 | message:[error localizedDescription] |
148 | cancelButtonTitle:@"OK" |
149 | otherButtonTitles:nil]; |