Search code examples
iosswiftmapboxwmsmapbox-ios

Mapbox WMS support for iOS


Is mapbox supporting WMS services? I found nothing in iOS documentation but in Android section I found this: https://docs.mapbox.com/android/maps/examples/add-a-wms-source/ . I've thired apply this code to iOS platform but my solution dosn't work. First problem that I met, was problem with construction of URL.

let url = URL(string: wms1)! 

Url constructor got problem with {bbox-epsg-3857} in passed string.

I ommited problem by allowing illegal characters:

let urlString = wms1.addingPercentEncoding(withAllowedCharacters: .illegalCharacters)
let url = URL(string: urlString!)! 

Then I tried add wms source to map but this provide some error

let source = MGLShapeSource(identifier: "test1", url: url, options: nil)
style.addSource(source)

let layer = MGLRasterStyleLayer(identifier: "test1", source: source)
style.addLayer(layer) 

Error: [ERROR] {}[Style]: Failed to load source test1:


Solution

  • While there is no specific example related to using a WMS as a RasterTileSource with the iOS Maps SDK on the Mapbox website, it is certainly possible to do so. The only requirement is that you initialize your source object with the proper Tile URL template. Aside from the url template, the implementation would be identical to this example: https://docs.mapbox.com/ios/maps/examples/source-custom-raster/

    The only difference is that you will need to modify this line:

    let source = MGLRasterTileSource(identifier: "stamen-watercolor", tileURLTemplates: ["https://stamen-tiles.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg"], options: [ .tileSize: 256 ])
    

    To reflect the Tile URL template of your WMS source, i.e.:

    let source = MGLRasterTileSource(identifier: "stamen-watercolor", tileURLTemplates: ["https://geodata.state.nj.us/imagerywms/Natural2015?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&width=256&height=256&layers=Natural2015"], options: [ .tileSize: 256 ])
    

    ⚠️ Disclaimer: I currently work for Mapbox ⚠️