在本章中我们将以危险品扩散分析举例,来介绍如何在ArcGIS API for iOS中调用GP服务。
1、按照前几章介绍的步骤来创建一个基本的地图应用程序,命名为GPDemo。
2、按照【ArcGIS API for iOS开发之旅】Graphic Layer中的步骤,定义一个GraphicsLayer并添加到地图视图中。
3、打开GPDemoViewController.h文件,在声明中添加AGSMapViewDelegate和AGSGeoprocessorDelegate,代码如下所示: @interface GPDemoViewController : UIViewController{
AGSMapView *mapView;
AGSGraphicsLayer *graphicsLayer;
}
4、打开GPDemoViewController.m文件,实现AGSMapViewDelegate的 -(void)mapview:(AGSMapView *)mapView didClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint graphics:(NSDictionary *)graphics方法。代码如下:
- (void)mapView:(AGSMapView *)mapViewdidClickAtPoint:(CGPoint)screen mapPoint:(AGSPoint *)mappoint graphics:(NSDictionary *)graphics
{
[self.graphicsLaye rremoveAllGraphics];
AGSPictureMarkerSymbol *pt = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:@"pushpin.png"];
// this offset is to line the symbol up with the map was actually clicked
pt.xoffset = 8;
pt.yoffset = -18;
// init pushpin with the AGSPictureMarkerSymbol we just created
AGSGraphic *pushpin = [[AGSGraphicalloc] initWithGeometry:mappoint symbol:pt attributes:nil infoTemplate:nil];
// add pushpin to graphics layer
[self.graphicsLayer addGraphic:pushpin];
// let the graphics layer know it needs to redraw
[pushpin release];
[self.graphicsLayer dataChanged];
AGSGeoprocessor *agp = [[AGSGeoprocesso ralloc] initWithURL:[NSURL URLWithString:kGPLayerURL]];
// set the delegate so we will be notified of delegate methods
agp.delegate = self;
// set the interval to check status to 10 seconds
agp.interval = 10;
agp.outputSpatialReference=self.mapView.spatialReference;
NSMutableArray *features = [[NSMutableArray alloc] init];
AGSGraphic *gra = [[AGSGraphicalloc] initWithGeometry:mappoint
symbol:nil
attributes:nil
infoTemplate:nil];
[features addObject:gra];
// get the parameters from the UI to submit
AGSFeatureSet *fs= [[[AGSFeatureSet alloc] init]initWithDisplayFieldName:nil
features:features
fieldAliases:nil
spatialReference:nil
geometryType:AGSGeometryPoint];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"Hydrogen sulfide",@"Material_Type",
@"45",@"Wind_Bearing__direction_blowing_to__0_-_360_",
@"Large",@"Large_or_Small_spill",
@"Day",@"Day_or_Night_incident",
fs, @"Incident_Point",
nil];
// submit the job
[agps ubmitJobWithParameters:params];
[agp release];
}
在上面代码中粗体部分定义了一个AGSGeoprocessor对象和参数,并使用异步方式调用。
5、实现AGSGeoprocessorDelegate中的方法,用来响应GP调用中以及调用结束后的方法,如下面代码:
- (void)geoprocessor:(AGSGeoprocessor *)geoprocessorwillCheckJobStatus:(NSString *)jobId {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)geoprocessor:(AGSGeoprocessor *)geoprocessordidCheckJobStatus:(NSString *)jobStatusforJob:(NSString *)jobId {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)geoprocessor:(AGSGeoprocessor *)geoprocessorjobDidFail:(NSString *)jobId status:(NSString *)jobStatus messages:(NSArray *)messages {
// update status
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"分析失败!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)geoprocessor:(AGSGeoprocessor *)geoprocessorjobDidSucceed:(NSString *)jobId status:(NSString *)jobStatus messages:(NSArray *)messages {
NSLog(@"Job succeeded");
// query the resulting html
[geoprocessor queryResultData:jobId paramName:@"outerg_shp"];
}
- (void)geoprocessor:(AGSGeoprocessor *)geoprocessordidQueryWithResult:(AGSGeoprocessorParameterValue *)result forJob:(NSString *)jobId {
inti = 0;
intguideNum = 0;
AGSFeatureSet * fs = result.value;
for (i=0;i<[fs.features count]; i++) {
AGSGraphic *gra = [fs.features objectAtIndex:i];
guideNum = [[gra.attributes objectForKey:@"ERGZone"] intValue];
switch (guideNum) {
case1:
{
AGSSimpleFillSymbol *sfsZone1 =[AGSSimpleFillSymbo lsimpleFillSymbol] ;
sfsZone1.color=[UIColor colorWithRed:255 green:208 blue:255 alpha:0.5];
sfsZone1.style = AGSSimpleFillSymbolStyleSolid;
gra.symbol = sfsZone1;
}
break;
case2:
{
AGSSimpleFillSymbol *sfsZone2 =[AGSSimpleFillSymbol simpleFillSymbol] ;
sfsZone2.style = AGSSimpleFillSymbolStyleSolid;
sfsZone2.color=[UIColor colorWithRed:255 green:0 blue:0 alpha:0.5];
gra.symbol = sfsZone2;
}
break;
case3:
{
AGSSimpleFillSymbol *sfsZone3 =[AGSSimpleFillSymbol simpleFillSymbol] ;
sfsZone3.style = AGSSimpleFillSymbolStyleSolid;
sfsZone3.color=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
gra.symbol = sfsZone3;
}
break;
default:
break;
}
[self.graphicsLayer addGraphic:gra];
}
[self.graphicsLayer dataChanged];
}
在上面代码中,- (void)geoprocessor:(AGSGeoprocessor *)geoprocessorjobDidSucceed:(NSString *)jobId status:(NSString *)jobStatus messages:(NSArray *)messages 函数在GP服务提交成功后响应,我们在该函数中去获取GP分析的结果,使用如下代码: [geoprocessorqueryResultData:jobIdparamName:@"outerg_shp"];
当获取结果成功后,会调用 - (void)geoprocessor:(AGSGeoprocessor *)geoprocessordidQueryWithResult:(AGSGeoprocessorParameterValue *)result forJob:(NSString *)jobId
方法,在该方法中,我们将得到的结果符号化后添加到graphicsLayer中。
6、通过上面几步操作,我们就完成了GP服务的使用,编译,运行,结果如下图: