Cannot display graphics on graphicslayer that are returned by AGSQueryTask query

2632
7
Jump to solution
10-12-2012 09:44 AM
OguzhanTopsakal
New Contributor III
I spent quite some time on this but could not figure out. Help is much appreciated..

I query a feature map as follows:

   self.queryTask = [AGSQueryTask queryTaskWithURL:featureURL];     self.queryTask.delegate = self;         AGSQuery *query = [AGSQuery query];     query.where = @"OBJECTID <= 100";     query.outFields = [NSArray arrayWithObject:@"*"];     query.returnGeometry = YES;     [self.queryTask executeWithQuery:query];


Everything works fine so far and I get response for 100 objects and add them to the graphicsLayer. graphic.geometry is not null.

- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {             [self.graphicsLayer removeAllGraphics];  for (AGSGraphic *graphic in featureSet.features) {                 graphic.visible = YES;   [self.graphicsLayer addGraphic:graphic];  }  [self.graphicsLayer dataChanged]; }



HOWEVER, I cannot see anything on the map. (I also tried the above code without removeAllGraphics)

The following code in another method works fine to add nodes on the map:
 AGSGraphic* myGraphic =[AGSGraphic graphicWithGeometry:myMarkerPoint              symbol:myMarkerSymbol             attributes:nodeDict            infoTemplateDelegate:template];       //Add the graphic to the Graphics layer  [self.graphicsLayer addGraphic:myGraphic];


Any ideas about what could be going wrong?

Thanks!
0 Kudos
1 Solution

Accepted Solutions
NimeshJarecha
Esri Regular Contributor
Okay, what is the spatial reference of the map (self.mapView.spatialReference)? What is the spatial reference of the geometries returned by query task?

If they are different then you should set out spatial reference to self.mapView.spatialReference of the query so server return geometries in map's spatial reference.

query.outSpatialReference = self.mapView.spatialReference;

Regards,
Nimesh

View solution in original post

0 Kudos
7 Replies
NimeshJarecha
Esri Regular Contributor
What type of renderer is set for the graphics layer? The returned graphics from query task ash which type of geometry? If render set on the graphics layer does not support geometry returned by query task then you should set symbol on each graphic before adding them to the graphics layer and it should draw it.

If returned graphics has polygon geometry then do this.

- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {    
        [self.graphicsLayer removeAllGraphics];
        
        AGSSimpleFillSymbol *simpleFillSymbol = [AGSSimpleFillSymbol simpleFillSymbol];

 for (AGSGraphic *graphic in featureSet.features) {
                graphic.visible = YES;
                graphic.symbol = simpleFillSymbol;
  [self.graphicsLayer addGraphic:graphic];
 }
 [self.graphicsLayer dataChanged];
}


Hope this helps!

Regards,
Nimesh
0 Kudos
OguzhanTopsakal
New Contributor III
Thanks for your reply.

The geometry is esriGeometryPolyline. I tried what you recommended but it didn't work. Then I checked graphicsLayer's renderer. It is null. Even though I tried the following to set the renderer, my self.graphicsLayer.renderer is still null.

    self.graphicsLayer.renderer = [[AGSSimpleRenderer simpleRendererWithSymbol:[AGSSimpleFillSymbol simpleFillSymbol]] retain];


Now my code is as follows:
- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {
    NSLog ( @"Renderer: %@", [self.graphicsLayer.renderer description]);
    NSLog( @" featureSet: %@", [featureSet description]);
    [self.graphicsLayer removeAllGraphics];
    AGSSimpleFillSymbol *simpleFillSymbol = [AGSSimpleFillSymbol simpleFillSymbol];
 for (AGSGraphic *graphic in featureSet.features) {
        graphic.visible = YES;
        graphic.symbol = simpleFillSymbol;
  [self.graphicsLayer addGraphic:graphic];
        NSLog(@" graphic: %@", [graphic description]);
 }
 [self.graphicsLayer dataChanged];
}


I get the following output:

Renderer: (null)

featureSet: AGSFeatureSet: display name: SEGMENT_PREFIX
geometry type: esriGeometryPolyline
num features: 100
spatial reference: AGSSpatialReference: wkid = 4326, wkt = null

graphic: geometry: AGSPolyline: [envelope: AGSEnvelope: xmin = -83.691725, ymin = 36.763704, xmax = -83.378514, ymax = 36.886530, spatial reference: [AGSSpatialReference: wkid = 4326, wkt = null]], symbol: <AGSSimpleFillSymbol: 0xacc3110>, attributes: {
    "ENG_REGION" = SOUTH;
    OBJECTID = 25;
    "SEGMENT_PREFIX" = 0SC;
    "SHAPE.LEN" = "41502.858489515296";
}, visible: 1


Any suggestions?



What type of renderer is set for the graphics layer? The returned graphics from query task ash which type of geometry? If render set on the graphics layer does not support geometry returned by query task then you should set symbol on each graphic before adding them to the graphics layer and it should draw it.

If returned graphics has polygon geometry then do this.

- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {    
        [self.graphicsLayer removeAllGraphics];
        
        AGSSimpleFillSymbol *simpleFillSymbol = [AGSSimpleFillSymbol simpleFillSymbol];

 for (AGSGraphic *graphic in featureSet.features) {
                graphic.visible = YES;
                graphic.symbol = simpleFillSymbol;
  [self.graphicsLayer addGraphic:graphic];
 }
 [self.graphicsLayer dataChanged];
}


Hope this helps!

Regards,
Nimesh
0 Kudos
NimeshJarecha
Esri Regular Contributor
I said, "If returned graphics has polygon geometry then do this." but since geometry is polyline you should create simple line symbol. Just use below code and it will handle all type of geometry.

- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {    
        [self.graphicsLayer removeAllGraphics];
        
        AGSSimpleFillSymbol *simpleFillSymbol = [AGSSimpleFillSymbol simpleFillSymbol];

 for (AGSGraphic *graphic in featureSet.features) {
                
                    if ([graphic.geometry isKindOfClass:[AGSPoint class]] || [graphic.geometry isKindOfClass:[AGSMultipoint class]]) {
                         graphic.symbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol];
                    }
                    else if ([graphic.geometry isKindOfClass:[AGSPolyline class]]) {
                         graphic.symbol = [AGSSimpleLineSymbol simpleLineSymbol];
                    }
                    else if ([graphic.geometry isKindOfClass:[AGSPolygon class]]) {
                         graphic.symbol = [AGSSimpleFillSymbol simpleFillSymbol];
                    }                
                    graphic.visible = YES;
                    [self.graphicsLayer addGraphic:graphic];
 }
 [self.graphicsLayer dataChanged];
}


Also, go through working with symbols and renderer documentation. That'll help you understand everything.

Regards,
Nimesh
0 Kudos
OguzhanTopsakal
New Contributor III
Thanks again for your answer. I read the documentation and I think everything I do (with your help) looks correct. However, it still do not work. I tried so many things..

To see if I can draw a line on the graphics layer, I tried the following and it worked. But it does not work with the graphics from queryTask.

        
        AGSMutablePolyline *polyline = [[AGSMutablePolyline alloc] initWithSpatialReference:self.mapView.spatialReference];
        [polyline addPathToPolyline];
        [polyline addPointToPath:myPoint1];
        [polyline addPointToPath:myPoint2];

        AGSGraphic *poly = [[AGSGraphic alloc] initWithGeometry:polyline
                                                         symbol:[AGSSimpleLineSymbol simpleLineSymbolWithColor:[UIColor blackColor]]
                                                     attributes:nil
                                           infoTemplateDelegate:nil];
        [self.graphicsLayer addGraphic:poly];
        [poly release];


Thanks for you time,
Regards.



I said, "If returned graphics has polygon geometry then do this." but since geometry is polyline you should create simple line symbol. Just use below code and it will handle all type of geometry.

- (void)queryTask:(AGSQueryTask *)queryTask operation:(NSOperation *)op didExecuteWithFeatureSetResult:(AGSFeatureSet *)featureSet {    
        [self.graphicsLayer removeAllGraphics];
        
        AGSSimpleFillSymbol *simpleFillSymbol = [AGSSimpleFillSymbol simpleFillSymbol];

 for (AGSGraphic *graphic in featureSet.features) {
                
                    if ([graphic.geometry isKindOfClass:[AGSPoint class]] || [graphic.geometry isKindOfClass:[AGSMultipoint class]]) {
                         graphic.symbol = [AGSSimpleMarkerSymbol simpleMarkerSymbol];
                    }
                    else if ([graphic.geometry isKindOfClass:[AGSPolyline class]]) {
                         graphic.symbol = [AGSSimpleLineSymbol simpleLineSymbol];
                    }
                    else if ([graphic.geometry isKindOfClass:[AGSPolygon class]]) {
                         graphic.symbol = [AGSSimpleFillSymbol simpleFillSymbol];
                    }                
                    graphic.visible = YES;
                    [self.graphicsLayer addGraphic:graphic];
 }
 [self.graphicsLayer dataChanged];
}


Also, go through working with symbols and renderer documentation. That'll help you understand everything.

Regards,
Nimesh
0 Kudos
NimeshJarecha
Esri Regular Contributor
Okay, what is the spatial reference of the map (self.mapView.spatialReference)? What is the spatial reference of the geometries returned by query task?

If they are different then you should set out spatial reference to self.mapView.spatialReference of the query so server return geometries in map's spatial reference.

query.outSpatialReference = self.mapView.spatialReference;

Regards,
Nimesh
0 Kudos
OguzhanTopsakal
New Contributor III
That's it! It worked.. You are the MAN! 🙂

Much appreciated,
Regards.

Okay, what is the spatial reference of the map (self.mapView.spatialReference)? What is the spatial reference of the geometries returned by query task?

If they are different then you should set out spatial reference to self.mapView.spatialReference of the query so server return geometries in map's spatial reference.

query.outSpatialReference = self.mapView.spatialReference;

Regards,
Nimesh
0 Kudos
NimeshJarecha
Esri Regular Contributor
Great! 🙂

Please mark thread as answered!

Regards,
Nimesh
0 Kudos