Search code examples
objective-carcgisnsurl

How to remove NSURL when a new NSURL is loaded


I have a pop over that list 11 different map layers and I want it to remove the previous selected layer when a new one is selected. How should I go about doing this. I have an if statement that changes the referencing url to the new layer. This is the code:

-(void) didLoadSelectedLayer:(NSString *)selectedLayer{
//Array of all the map layers
self.popOverArray = [[NSArray alloc] initWithObjects:@"Electric Radio", @"Electric Truck", @"Gas Radio", @"Gas Truck", @"Meter", @"Sewer Radio", @"Sewer Truck", @"Support Radio", @"Support Truck", @"Water Radio", @"Water Truck", nil];
//Load layers based on selected cell
if ([selectedLayer isEqualToString:@"Electric Radio"]) {

    _url = [NSURL URLWithString:@"https://example.com./0"];
}else if ([selectedLayer isEqualToString:@"Electric Truck"]){

    _url = [NSURL URLWithString:@"https://example.com/1"];
}else if ([selectedLayer isEqualToString:@"Gas Radio"]){
    _url = [NSURL URLWithString:@"https://example.com/2"];
}else if ([selectedLayer isEqualToString:@"Gas Truck"]){
    _url = [NSURL URLWithString:@"https://example.com/2"];
}else if ([selectedLayer isEqualToString:@"Meter"]){
    _url = [NSURL URLWithString:@"example.com/4"];
}else if ([selectedLayer isEqualToString:@"Sewer Radio"]){
    _url = [NSURL URLWithString:@"example.com/5"];
}else if ([selectedLayer isEqualToString:@"Sewer Truck"]){
    _url = [NSURL URLWithString:@"https://eample.com/6"];
}else if ([selectedLayer isEqualToString:@"Support Radio"]){
    _url = [NSURL URLWithString:@"https://example.com/7"];
}else if ([selectedLayer isEqualToString:@"Support Truck"]){
    _url = [NSURL URLWithString:@"https://example.com/8"];
}else if ([selectedLayer isEqualToString:@"Water Radio"]){
    _url = [NSURL URLWithString:@"https://example.com/9"];
}else if ([selectedLayer isEqualToString:@"Water Truck"]){
    _url = [NSURL URLWithString:@"https://example.com/10"];
}else {

}
//Load the Selected layer to map view
_graphicsLayer = [AGSGraphicsLayer graphicsLayer];
[self.mapView addMapLayer:_graphicsLayer withName:@"Graphics Layer"];
AGSCredential *userCred = [[AGSCredential alloc] initWithUser:@"username" password:@"password"];
AGSFeatureLayer *featrueServiceLayer = [[AGSFeatureLayer alloc] initWithURL:_url  mode:AGSFeatureLayerModeOnDemand credential:userCred];

[featrueServiceLayer setOutFields:[NSArray arrayWithObject:@"*"]];
featrueServiceLayer.editingDelegate = self;

[self.mapView addMapLayer:featrueServiceLayer withName:@"featureService"];


}

Solution

  • after you add the graphicslayer create an if-statement to see if one has been added and remove the graphics layer if it is true and then remove featureservice.

     _graphicsLayer = [AGSGraphicsLayer graphicsLayer];
    
    if ([_graphicsLayer graphics]) {
        [_graphicsLayer removeAllGraphics];
        [self.mapView removeMapLayerWithName:@"featureService"];
    }