首页 > 解决方案 > Mapbox WMS 对 iOS 的支持

问题描述

mapbox 是否支持 WMS 服务?我在 iOS 文档中什么也没找到,但在 Android 部分我发现了这个:https ://docs.mapbox.com/android/maps/examples/add-a-wms-source/ 。我已经将此代码应用于 iOS 平台,但我的解决方案不起作用。我遇到的第一个问题是 URL 的构建问题。

let url = URL(string: wms1)! 

Url 构造函数在传递的字符串中遇到 {bbox-epsg-3857} 问题。

我通过允许非法字符省略了问题:

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

然后我尝试将 wms 源添加到地图,但这提供了一些错误

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

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

错误:[ERROR] {}[Style]:无法加载源 test1:

标签: iosswiftmapboxwmsmapbox-ios

解决方案


虽然在 Mapbox 网站上没有与 iOS Maps SDK 一起使用 WMS 作为 RasterTileSource 的具体示例,但确实可以这样做。唯一的要求是使用正确的 Tile URL 模板初始化源对象。除了 url 模板之外,实现将与此示例相同:https ://docs.mapbox.com/ios/maps/examples/source-custom-raster/

唯一的区别是您需要修改这一行:

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

反映您的 WMS 源的 Tile URL 模板,即:

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 ])

⚠️ 免责声明:我目前为 Mapbox 工作⚠️


推荐阅读