Skip To Content

Use ArcGIS basemaps

In this topic

This tutorial describes how to build an OS X application that can display a range of basemaps. It's assumed that you've already added a map to your application as described in this Add a map to your app topic.

The API provides a number of classes that allow you display basemaps from various sources, such as ArcGIS map services, Bing Maps, Open Street Map, and local tile packages. Learn more about layers

In this tutorial you will be able to do the following:

  • Write code to add and remove basemaps
  • Add a segmented control to the UI to allow users to display different basemaps.
  • Explore some of the map services that are freely available on ArcGIS Online.

1. Add a segmented control to the UI

Add a segmented control (NSSegmentedControl) into the window and set the number of segments to 5. Change each segment label so that there are options for 'Gray', 'Oceans', Nat. Geo.','Topo' and 'Satellite'. In the next step you will program each of these segments to add a basemap.

NSSegmentedControl

Next, connect the segmented control to an IBAction, and provide a name such as basemapChanged. This will allow the app to respond whenever the user selects a new segment on the control.

IBAction

Lastly, connect the segmented control to an IBOutlet.

2. Allow the user to switch basemaps

Next add the logic to switch the basemaps. Each time a user selectes a segment the existing basemap is removed, a new URL object is created pointing to a predefined map service, then a tiled map service layer is instantiated and added to the map.

This is all carried out using the basemapChanged: method that is fired when the segmented control is clicked.

@IBAction func basemapChanged(sender: NSSegmentedControl) {
    
 //remove the existing basemap layer
 self.mapView.removeMapLayerWithName("Basemap Tiled Layer")
  
 //define the URL for the predefined basemap
 var basemapURL:NSURL        
 switch sender.selectedSegment {
 case 0:  //gray
  basemapURL = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer")!
 case 1:  //oceans
  basemapURL = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer")!
 case 2:  //nat geo
  basemapURL = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer")!
 case 3:  //topo
  basemapURL = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer")!
 default:  //sat
  basemapURL = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer")!
 }
        
 //Instantiate a tiled map service layer using the URL
 var newBasemapLayer = AGSTiledMapServiceLayer(URL: basemapURL)
 newBasemapLayer.name = "Basemap Tiled Layer"

 //add this layer at the bottom of the layers
 self.mapView.insertMapLayer(newBasemapLayer, atIndex: 0)
}

3. Run the application

And that's it. You've written all the code required to switch the basemap layer of a map. All you need to do now is run the application. It should look like this.

Use ArcGIS basemaps