FeatureLayer initWithJson

3461
7
03-15-2011 06:45 PM
AdamPfister
Esri Contributor
I'm trying to build a feature layer using a JSON string but there is no point showing on the simulator when I run the project. Am I going about it the correct way?

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *serviceUrl = [NSURL URLWithString:kMapServiceURL];
    AGSTiledMapServiceLayer *tiledMapServiceLayer = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:serviceUrl];
    [self.mapView addMapLayer:tiledMapServiceLayer withName:@"World Street Map"];
    
    //test FL w/ JSON
    NSString *featureCollection = @"{\"layerDefinition\": { \"geometryType\": \"esriGeometryPoint\"  }, \"featureSet\": { \"features\": [ { \"attributes\": { \"key\": \"someValue\"}, \"geometry\": {\"x\":-122.408951,\"y\":37.783206,\"spatialReference\":{\"wkid\":4326}}  } ] } }";
    
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *json = [parser objectWithString:featureCollection];
    
    AGSFeatureLayer *featureLayer = [[AGSFeatureLayer alloc] initWithJSON:json];
    
    [self.mapView addMapLayer:featureLayer withName:@"FL"];
 
}


Thanks,
Adam
0 Kudos
7 Replies
PaulLohr
Occasional Contributor III
I am fairly new to Objective-C so please take this for what it is worth.

I would first check if that long string makes it into your featureCollection NSString object. You probably know this, but you can try NSLogging featureCollection - does the string appear as you expect it in the console?
0 Kudos
AdamPfister
Esri Contributor
I threw a breakpoint in after it loaded up the json and then printed it out.  Then i went here, http://jsonformatter.curiousconcept.com/, and everything came back ok so i'm pretty sure the JSON is formatted correctly.  I wonder if there is some key/value pair I'm missing that is required for the FeatureLayer to properly serialize?
0 Kudos
ClayTinnell
New Contributor III
Adam,

I don't think this is actually the intent of the initWithJSON constructor. This constructor returns a Feature Layer based upon a JSON representation of the feature attributes.  Here is an example:
feature layer json {
    currentVersion = 0;
    drawingInfo =     {
    };
    editable = false;
    geometryType = esriGeometryPoint;
    hasAttachments = false;
    id = 0;
    maxScale = 0;
    minScale = 0;
}

What exactly are you looking to do?
0 Kudos
AdamPfister
Esri Contributor
I'm trying to construct a FeatureLayer from a stored JSON string in the same way it's done in the JavaScript API shown here: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/featurelayer.htm#Featu....

Is this possible w/ the iOS API?

thanks,
Adam
0 Kudos
ClayTinnell
New Contributor III
This may get you a little closer.  I still don't see the point, but I am getting the feature to show up in the feature layer.  Note... I adjusted the json a little and changed the point and the spatial reference to a known point:

NSString *ld = @"{ \"geometryType\": \"esriGeometryPoint\"  }";
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *layerDefinition = [parser objectWithString:ld];

NSString *fs = @"{ \"features\": [ { \"attributes\": { \"key\": \"someValue\"}, \"geometry\": {\"x\":-9407733.852498,\"y\":4735858.166659,\"spatialReference\":{\"wkid\":102100}}  } ] }";
   
NSDictionary *featureSet = [parser objectWithString:fs];
AGSFeatureLayer *featureLayer = [[AGSFeatureLayer alloc] initWithLayerDefinitionJSON:layerDefinition featureSetJSON:featureSet];
featureLayer.outFields = [NSArray arrayWithObject:@"*"];
[self.mapView addMapLayer:featureLayer withName:@"FL"];

NSLog(@"features: %@ ",featureLayer.graphics);
AGSPoint *point = [AGSPoint pointWithX:-9407733.852498 y:4735858.166659 spatialReference:self.mapView.spatialReference];
AGSEnvelope *extent = [AGSEnvelope envelopeWithXmin:point.x ymin:point.y xmax:point.x+50 ymax:point.y+50 spatialReference:mapView.spatialReference];

[self.mapView zoomToEnvelope:extent animated:YES];
0 Kudos
AdamPfister
Esri Contributor
What I'm trying to do is just see what it would take to create a simple FL that will always be constant within the application.  If I can create the FL from a JSON string without having to add it from an actual data source like a MapService, it may be beneficial for me.  Just trying things out as I'm new to the iOS scene.

Thanks for your help and I'll give that a try and report back.

Adam
0 Kudos
AdamPfister
Esri Contributor
Looks like that worked.  Thanks!
0 Kudos