English

How To: Consume open source (WMS and WMTS) services with the ArcGIS Runtime SDK for iOS

Summary

The ArcGIS Runtime SDK for iOS meets OGC standards for web map services (WMS) and web map tiled services (WMTS). These services can be hosted on ArcGIS Server, or on open source servers like GeoServer.

Debugging these applications can be tricky. It is recommended to use a web debugger, either on a Mac or on a PC as a proxy. These applications work whether the testing is done on a physical iOS device or a simulator. Some examples are:

Charles - Web Debugging Proxy Application for Windows, Mac OS and Linux

Fiddler - Web Debugging Proxy (PC)

▪ ▪ Fiddler documentation: Capture Traffic on a PC from an iOS Device

Procedure

WMS
Provide the URL to the WMS service. This may vary depending on the server structure. Here are two examples of valid URLs:

• This is an instance of GeoServer


NSURL *url = [NSURL URLWithString:@
"http://demo.opengeo.org/geoserver/wms"];

• This is an instance of ArcGIS Server

NSURL *url = [NSURL URLWithString:@
"http://sampleserver6.arcgisonline.com/arcgis/services/911CallsHotspot/MapServer/WMSServer"];

• To add the WMS to the map, use these lines of code:

AGSWMSLayer *wmsLayer = [AGSWMSLayer wmsLayerWithURL:url];
[self.mapView addMapLayer:wmsLayer withName:@"AnyName"];

• The 'withName' refers to a relative name for the WMS layer. This is not the actual name, but a relative one used to keep track of the layer in case it is necessary to modify or retrieve it later.

• To add, or exclude, specific layers in a Map Service, it is necessary to specify the number of the layer of interest as an object in an array.

For example, if only the second layer in a Map Service is to be shown in the map, the Map Service is (0), the first layer is (1), and the second layer is (2):
wmsLayer.visibleLayers = [NSArray arrayWithObject:@"2"];

Sample WMS application file:


// ViewController.m
// MyFirstMapApp
// Copyright (c) 2013 Esri. All rights reserved.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//GPS location function, comment-out to remove location display (blue dot)
- (void) mapViewDidLoad:(AGSMapView *)mapView
{
// [mapView.locationDisplay startDataSource];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

NSURL *url1 = [NSURL
URLWithString:@"http://demo.opengeo.org/geoserver/wms"];

NSURL *url2 = [NSURL URLWithString:@"http://sampleserver6.arcgisonline.com/arcgis/services/911CallsHotspot/MapServer/WMSServer"];

AGSWMSLayer *wmsLayer1 = [AGSWMSLayer wmsLayer1WithURL:url1];
AGSWMSLayer *wmsLayer2 = [AGSWMSLayer wmsLayer2WithURL:url2];

[self.mapView addMapLayer:wmsLayer1 withName:@"AnyName"];

[self.mapView addMapLayer:wmsLayer2 withName:@"AnyName"];
wmsLayer2.visibleLayers = [NSArray arrayWithObject:@"2"];

self.mapView.layerDelegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

WMTS
In the '.h' file, provide the URL to the WMTS service. This may vary depending on the server structure. Here is an example of a valid URL:

• This is an instance of ArcGIS Server


#define wmtsURL @" http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS"

1. Add the AGSWMTSInfoDelegate to the ViewController interface:

@interface ViewController : UIViewController <AGSMapViewLayerDelegate, AGSWMTSInfoDelegate> {
AGSMapView *_mapView;}

2. Add properties to the WMTS layer:

@property (strong, nonatomic) AGSWMTSInfo *wmtsInfo;
@property (strong, nonatomic) AGSWMTSLayer *wmtsLayer;

3. In the '.m' file, add the WMTS to the map:

self.wmtsInfo = [[AGSWMTSInfo alloc] initWithURL: [NSURL URLWithString:wmtsURL]];
self.wmtsInfo.delegate = self;

Sample WMTS application file (ViewController.h):


// ViewController.h
// MyFirstMapApp
// Copyright (c) 2013 Esri. All rights reserved.

#import <UIKit/UIKit.h>
#import <ArcGIS/ArcGIS.h>
//spatial reference is 102100 (3857)
#define wmtsURL @" http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS"

@interface ViewController : UIViewController <AGSMapViewLayerDelegate, AGSWMTSInfoDelegate> {
//container for map layers
AGSMapView *_mapView;
}
//map view is an outlet so we can associate it with UIView
@property (strong, nonatomic) IBOutlet AGSMapView *mapView;

//add WMTS properties...
@property (strong, nonatomic) AGSWMTSInfo *wmtsInfo;
@property (strong, nonatomic) AGSWMTSLayer *wmtsLayer;

@end

Sample WMTS application file(ViewController.m):


// ViewController.m
// MyFirstMapApp
// Copyright (c) 2013 Esri. All rights reserved.

#import "ViewController.h"
@implementation ViewController
@synthesize mapView = _mapView;
//GPS location function, comment-out to remove location display (blue dot)
- (void) mapViewDidLoad:(AGSMapView *)mapView
{
// [mapView.locationDisplay startDataSource];
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.mapView.layerDelegate = self;
self.wmtsInfo = [[AGSWMTSInfo alloc] initWithURL: [NSURL URLWithString:wmtsURL]];
self.wmtsInfo.delegate = self;

// Do any additional setup after loading the view, typically from a nib.
}

- (void) wmtsInfoDidLoad:(AGSWMTSInfo *) wmtsInfo{
NSArray *layerInfos = [wmtsInfo layerInfos];
AGSWMTSLayerInfo *layerInfo = [layerInfos objectAtIndex:0];
self.wmtsLayer = [wmtsInfo wmtsLayerWithLayerInfo:layerInfo andSpatialReference:nil];
[self.mapView addMapLayer:self.wmtsLayer withName:@"wmts Layer"];
}

// Show error message if something goes wrong
- (void) wmtsInfo:(AGSWMTSInfo *)wmtsInfo didFailToLoad:(NSError *)error {
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Related Information