Select to view content in your preferred language

Different base map with different feature layers

968
3
Jump to solution
11-15-2016 12:43 PM
MuhammadRafay
Deactivated User

I am toggling different base map in my application with different feature layers. The problem is that when I toggle on any feature layer on current base map and then change the base map, the feature layer disappears. Is there any way to to just change the base map without doing anything to feature layer? or just notify the feature layer that the underlying base map has been changed and feature layer render itself?

0 Kudos
1 Solution

Accepted Solutions
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

You can use this Featurelayer method to set the visibility 10.2.5: AGSLayer Class Reference 

Feel free to mark this question as answered. 

View solution in original post

3 Replies
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Muhammad,

I think the issue is the order of layer when you add into the mapview.  Maps and layers—ArcGIS Runtime SDK for iOS | ArcGIS for Developers 

Here I shared with you a sample code to show how to make it works with two toggles, one control basemap layer, and the other control featurelayer.

The key part is you should use insertMapLayer:atIndex: method rather than just simply addLayer. 

You want to always make sure the basemap is always atIndex 0, which means the first layer add to the mapview, then all the featurelayer later can stack on top of it.

@IBOutlet weak var mapView: AGSMapView!
@IBOutlet weak var basemapSwitch: UISwitch!
@IBOutlet weak var featureLayerSwitch: UISwitch!
    
let featureLayer1 = AGSFeatureLayer(URL: NSURL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0"), mode: .OnDemand)
let featureLayer2 = AGSFeatureLayer(URL: NSURL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/0"), mode: .OnDemand)
    
let worldTopoBasemap = AGSTiledMapServiceLayer(URL: NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"))
let worldImageryBasemap = AGSTiledMapServiceLayer(URL: NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"))
        
override func viewDidLoad() {
        super.viewDidLoad()
        
        // set the delegate for the map view
        self.mapView.layerDelegate = self
        
        self.basemapSwitch.setOn(false, animated: false)
        self.featureLayerSwitch.setOn(false, animated: false)
        
        //Add it to the map view by default with one basemap and layer
        self.mapView.addMapLayer(worldTopoBasemap, withName: "Tiled Layer")
        self.mapView.addMapLayer(featureLayer1, withName: "featureLayer1")        
    }

@IBAction func switchBasemapToggle(sender: UISwitch) {
        if basemapSwitch.on {
            print("SwitchBasemap toggle is on")
            self.mapView.removeMapLayer(worldTopoBasemap)
            self.mapView.insertMapLayer(worldImageryBasemap, atIndex: 0)
        } else {
            print("SwitchBasemap toggle is off")
            print(self.mapView.mapLayers.count)
            self.mapView.removeMapLayer(worldImageryBasemap)
            self.mapView.insertMapLayer(worldTopoBasemap, atIndex: 0)
        }
    }
    
@IBAction func switchFeatureLayerToggle(sender: UISwitch) {
        if featureLayerSwitch.on {
            print("featureLayerSwitch toggle is on")
            self.mapView.removeMapLayer(featureLayer1)
            self.mapView.insertMapLayer(featureLayer2, atIndex: 1)
        } else {
            print("featureLayerSwitch toggle is off")
            print(self.mapView.mapLayers.count)
            self.mapView.removeMapLayer(featureLayer2)
            self.mapView.insertMapLayer(featureLayer1, atIndex: 1)
        }
    }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

And here is a demo gif:

Demo to toggle featureLayer and basemap

Hope this can help.

MuhammadRafay
Deactivated User

thank you Yue, just have one more question. Do we always need to remove the feature layer in order to hide it? Or is there any API provided by ArcGIS which could just toggle the visibility of feature layer?

0 Kudos
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

You can use this Featurelayer method to set the visibility 10.2.5: AGSLayer Class Reference 

Feel free to mark this question as answered.