ArcGIS API for iOS开发教程(五)数据查询

来源:互联网 发布:微信里的数据能清除吗 编辑:程序博客网 时间:2024/04/20 23:28

http://www.giser.net/?p=23

在大量数据及信息面前,如何获得符合自己所需的数据及信息是非常必要且重要的。同其他WebAPIs 一样,ArcGIS API for iOS中同样可以使用queryTask来进行数据查询,并且使用方式也一致。下面我们以一实例来进行详细介绍。
1、按照前几章介绍的步骤来创建一个基本的地图应用程序,这里我们将其命名为QueryDemo。
2、按照【ArcGIS API for iOS开发之旅】Graphic Layer中的步骤,定义一个GraphicsLayer并添加到地图视图中。
3、打开QueryDemoViewController.h文件。在QueryDemoViewController类中定义AGSQueryTask 对象和AGSQuery对象,并声明为QueryDemoViewController类的属性。此外,在声明中添加 AGSQueryTaskDelegate。代码如下

  1. @interface QueryDemoViewController: UIViewController {
  2. AGSMapView       *mapview;
  3. AGSQueryTask      *queryTask;
  4. AGSQuery                *query;
  5. AGSGraphicsLayer    *graphicsLayer;
  6. }
  7. @property(nonatomic, retain) IBOutlet AGSMapView   *mapView;
  8. @property(nonatomic, retain) IBOutlet AGSQueryTask     *queryTask;
  9. @property(nonatomic, retain) IBOutlet AGSQuery   *query;
  10. @property(nonatomic, retain) IBOutlet AGSGraphicsLayer   *graphicsLayer;

4、打开QueryDemoViewController.m文件,完成queryTask和query的属性定义。代码如下

  1. @implementation QueryDemoViewController
  2. @synthesize mapView;
  3. @synthesize queryTask;
  4. @synthesize query;
  5. @synthesize graphicsLayer;

5、在viewDidLoad函数中,初始化queryTask和query,并执行查询操作。代码如下

  1. -(void)viewDidLoad{
  2. [super viewDidLoad];
  3. self.mapView.mapViewDelegate = self;
  4. AGSTitledMapServiceLayer *tiledLayer = [[AGSTiledMapServiceLayer alloc]initWithURL:[NSURL URLWithString:kTiledMapServiceURL]];
  5. [self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];
  6. [tiledLayer release];
  7. self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
  8. [self.mapView addMapLayer: self.graphicsLayer withName:@"graphicsLayer"];
  9. NSString *countiesLayerURL = kMapServiceLayerURL;
  10. //set up query task against layer, specify the delegate
  11. self.queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:countiesLayerURL];
  12. self.queryTask.delegate = self;
  13. //return all fields in query
  14. self.query = [AGSQuery query];
  15. self.query.outFields = [NSArray arrayWithObjects:@"*",nil];
  16. self.query.returnGeometry = YES;
  17. self.query.text = @”California”;
  18. [self.queryTask executeWithQuery:self.query];
  19. }

6、添加AGSQueryTaskDelegate中的查询响应函数。代码如下
// 在Query成功后响应,将查询结果显示到graphicsLayer上

  1. -(void)queryTask: (AGSQueryTask*) queryTask operation:(NSOperation*) opdidExecuteWithFeatureSetResult:(AGSFeatureSet*) featureSet{
  2. //get feature, and load in to table
  3. Int i = 0;
  4. for(i=0; i<[featureSet.featurescount]; i++)
  5. {
  6. AGSSimpleFillSymbol *fillSym = [AGSSimpleFillSymbol simpleFillSymbol];
  7. fillSym.style = AGSSimpleFillSymbolStyleSolid;
  8. fillSym.color = [UIColororangeColor];
  9. AGSGraphic *gra = [featureSet.features objectAtIndex:i];
  10. gra.symbol = fillSym;
  11. [self.graphicsLayer addGraphic:gra];
  12. }
  13. [self.graphicsLayer dataChanged];
  14. }
  15. //if there’s an error with the query display it to the uesr 在Query失败后响应,弹出错误提示框
  16. -(void)queryTask: (AGSQueryTask*)queryTask operation:(NSOperation*)opdidFailWithError:(NSError*)error{
  17. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  18. UIAlertView *alertView = [UIAlertView alloc]initWithTitle:@”Error”
  19. message:[error localizedDescription]
  20. delegate:nil
  21. cancelButtonTitle:@”OK”
  22. otherButtonTitles:nil];
  23. [alertView show];
  24. [alertView release];
  25. }

7、编译、执行。结果如下图所示:

原创粉丝点击