在大量数据及信息面前,如何获得符合自己所需的数据及信息是非常必要且重要的。同其他WebAPIs 一样,ArcGIS API for iOS中同样可以使用queryTask来进行数据查询,并且使用方式也一致。下面我们以一实例来进行详细介绍。
1、按照前几章介绍的步骤来创建一个基本的地图应用程序,这里我们将其命名为QueryDemo。
2、按照【ArcGIS API for iOS开发之旅】Graphic Layer中的步骤,定义一个GraphicsLayer并添加到地图视图中。
3、打开QueryDemoViewController.h文件。在QueryDemoViewController类中定义AGSQueryTask 对象和AGSQuery对象,并声明为QueryDemoViewController类的属性。此外,在声明中添加 AGSQueryTaskDelegate。代码如下
- @interface QueryDemoViewController: UIViewController {
- AGSMapView *mapview;
- AGSQueryTask *queryTask;
- AGSQuery *query;
- AGSGraphicsLayer *graphicsLayer;
- }
- @property(nonatomic, retain) IBOutlet AGSMapView *mapView;
- @property(nonatomic, retain) IBOutlet AGSQueryTask *queryTask;
- @property(nonatomic, retain) IBOutlet AGSQuery *query;
- @property(nonatomic, retain) IBOutlet AGSGraphicsLayer *graphicsLayer;
4、打开QueryDemoViewController.m文件,完成queryTask和query的属性定义。代码如下
- @implementation QueryDemoViewController
- @synthesize mapView;
- @synthesize queryTask;
- @synthesize query;
- @synthesize graphicsLayer;
5、在viewDidLoad函数中,初始化queryTask和query,并执行查询操作。代码如下
- -(void)viewDidLoad{
- [super viewDidLoad];
- self.mapView.mapViewDelegate = self;
- AGSTitledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc]initWithURL:[NSURL URLWithString:kTiledMapServiceURL]];
- [self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];
- [tiledLayer release];
- self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
- [self.mapView addMapLayer: self.graphicsLayer withName:@"graphicsLayer"];
- NSString *countiesLayerURL = kMapServiceLayerURL;
- //set up query task against layer, specify the delegate
- self.queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:countiesLayerURL];
- self.queryTask.delegate = self;
- //return all fields in query
- self.query = [AGSQuery query];
- self.query.outFields = [NSArray arrayWithObjects:@"*",nil];
- self.query.returnGeometry = YES;
- self.query.text = @”California”;
- [self.queryTask executeWithQuery:self.query];
- }
6、添加AGSQueryTaskDelegate中的查询响应函数。代码如下
// 在Query成功后响应,将查询结果显示到graphicsLayer上
- -(void)queryTask: (AGSQueryTask*) queryTask operation:(NSOperation*) opdidExecuteWithFeatureSetResult:(AGSFeatureSet*) featureSet{
- //get feature, and load in to table
- Int i = 0;
- for(i=0; i<[featureSet.featurescount]; i++)
- {
- AGSSimpleFillSymbol *fillSym = [AGSSimpleFillSymbol simpleFillSymbol];
- fillSym.style = AGSSimpleFillSymbolStyleSolid;
- fillSym.color = [UIColororangeColor];
- AGSGraphic *gra = [featureSet.features objectAtIndex:i];
- gra.symbol = fillSym;
- [self.graphicsLayer addGraphic:gra];
- }
- [self.graphicsLayer dataChanged];
- }
- //if there’s an error with the query display it to the uesr 在Query失败后响应,弹出错误提示框
- -(void)queryTask: (AGSQueryTask*)queryTask operation:(NSOperation*)opdidFailWithError:(NSError*)error{
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- UIAlertView *alertView = [UIAlertView alloc]initWithTitle:@”Error”
- message:[error localizedDescription]
- delegate:nil
- cancelButtonTitle:@”OK”
- otherButtonTitles:nil];
- [alertView show];
- [alertView release];
- }
7、编译、执行。结果如下图所示: