how to use a Class Breaks Renderer in the ESRI ArcGIS iPhone API to display cities of varying population with different

Code Listings

ClassBreaksViewController.h

01#import <UIKit/UIKit.h>
02#import "AGSiPhone.h"
03 
06 
07@interface ClassBreaksViewController : UIViewController<AGSMapViewDelegate, AGSQueryTaskDelegate> {
08    AGSMapView *mapView;
09    AGSGraphicsLayer *cityGraphicsLayer;
10    AGSQueryTask *cityQueryTask;
11     
12}
13 
14@property (nonatomic, retain) IBOutlet AGSMapView *mapView;
15@property (nonatomic, retain) AGSGraphicsLayer *cityGraphicsLayer;
16@property (nonatomic, retain) AGSQueryTask *cityQueryTask;
17 
18@end

ClassBreaksViewController.m

001#import "ClassBreaksViewController.h"
002 
003@implementation ClassBreaksViewController
004 
005@synthesize mapView;
006@synthesize cityGraphicsLayer;
007@synthesize cityQueryTask;
008 
009// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
010- (void)viewDidLoad {
011    [super viewDidLoad];
012     
013    // Set map view delegate
014    self.mapView.mapViewDelegate = self;
015     
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];
020     
021    // Create grpahics layer
022    self.cityGraphicsLayer = [AGSGraphicsLayer graphicsLayer];
023     
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;
028    lowSymbol.size = 15;
029     
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;
034     
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;
039     
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"];
044     
045    // Create three AGSClassBreak objects, one each for low, medium and high populations
046    AGSClassBreak* lowClassBreak = [AGSClassBreak
047                                    classBreakInfoWithSymbol:lowSymbol forMinValue:DBL_MIN
048                                    maxValue:50000.0];
049     
050    AGSClassBreak* mediumClassBreak = [AGSClassBreak
051                                       classBreakInfoWithSymbol:mediumSymbol forMinValue:50000.0
052                                       maxValue:250000];
053     
054    AGSClassBreak* highClassBreak = [AGSClassBreak
055                                     classBreakInfoWithSymbol:highSymbol forMinValue:250000.0
056                                     maxValue:DBL_MAX];
057     
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];
062     
063    // Add the renderer to the graphics layer
064    self.cityGraphicsLayer.renderer = cityRenderer;
065     
066    // Add the graphics layer to the map view
067    [self.mapView addMapLayer:self.cityGraphicsLayer withName:@"CityGraphicsLayer"];
068}
069 
070// Override to allow orientations other than the default portrait orientation.
071- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
072    // Return YES for supported orientations
073    return YES;
074}
075 
076- (void)didReceiveMemoryWarning {
077    // Releases the view if it doesn't have a superview.
078    [super didReceiveMemoryWarning];
079     
080    // Release any cached data, images, etc that aren't in use.
081}
082 
083- (void)viewDidUnload {
084    // Release any retained subviews of the main view.
085    // e.g. self.myOutlet = nil;
086    self.cityGraphicsLayer = nil;
087    self.mapView = nil;
088    [super viewDidUnload];
089}
090 
091- (void)dealloc {
092    [cityGraphicsLayer release];
093    [mapView release];
094    [super dealloc];
095}
096 
097#pragma mark AGSMapViewDelegate
098 
099// Called when the map view is loaded (after the view is loaded)
100- (void)mapViewDidLoad:(AGSMapView *)mapView {
101         
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;
105     
106    AGSQuery *cityQuery = [AGSQuery query];
107    cityQuery.where = @"STATE_NAME = 'California'";
108    cityQuery.outFields = [NSArray arrayWithObject:@"*"];
109         
110    [self.cityQueryTask executeWithQuery:cityQuery];
111     
112    // Create extent to be used as default
113    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-118.6
114                                                     ymin:33.6
115                                                     xmax:-118.1
116                                                     ymax:34.2
117                                         spatialReference:self.mapView.spatialReference];
118     
119    // Call method to set extent, pass in envelope
120    [self.mapView performSelector:@selector(zoomToEnvelope:animated:)
121                       withObject:envelope
122                       afterDelay:0.5];   
123}   
124 
125#pragma mark AGSQueryTaskDelegate
126 
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];
132    }
133    [self.cityGraphicsLayer dataChanged];
134     
135    // Clean up query task memory
136    self.cityQueryTask = nil;
137}
138 
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;
143 
144    // Display error message
145    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
146                                                    message:[error localizedDescription]
147                                                   delegate:nil
148                                          cancelButtonTitle:@"OK"
149                                          otherButtonTitles:nil];
150    [alert show];
151    [alert release];
152}
153 
154@end
原文地址:https://www.cnblogs.com/moonvan/p/2184301.html