swift Map框填充层未渲染

u3r8eeie  于 2022-12-22  发布在  Swift
关注(0)|答案(1)|浏览(140)
    • bounty将在3天后过期**。回答此问题可获得+100声望奖励。nickcoding2希望引起更多人对此问题的关注:需要一个可行的解决方案。不想只编辑Map以在加载时包含geojson。想在渲染Map时添加图层。

在MapBox中,我试图在Map中高亮显示GeoJSON的一个区域,例如我找到的"Manhattan,NYC" GeoJSON。下面是我的ViewDidLoad()函数,但FillLayer似乎没有在Map上渲染,我不知道为什么...

override public func viewDidLoad() {
    super.viewDidLoad()
    let myResourceOptions = ResourceOptions(accessToken: "my_key_here")
    let myMapInitOptions = MapInitOptions(resourceOptions: myResourceOptions, styleURI: StyleURI(url: URL(string: "mapbox://styles/custom_URL_here")!)!)
    
    mapView = MapView(frame: view.bounds, mapInitOptions: myMapInitOptions)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    let geoJSONDataSourceIdentifier = "SOURCE_ID"
    var geoJSONSource = GeoJSONSource()
    geoJSONSource.data = .url(URL(string: "https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/manhattan.geojson")!)
    try! mapView.mapboxMap.style.addSource(geoJSONSource, id: geoJSONDataSourceIdentifier)
    mapView.mapboxMap.onNext(event: .mapLoaded) { _ in
        var polygonLayer = FillLayer(id: "LAYER_ID")
        polygonLayer.source = "SOURCE_ID"
        polygonLayer.fillColor = .constant(StyleColor(.green))
        polygonLayer.fillOpacity = .constant(1)
        try! self.mapView.mapboxMap.style.addLayer(polygonLayer)
    }
    self.view.addSubview(mapView)
}

我得到一个错误:'无法为ID为的源设置数据:源ID,错误:样式错误(原始值:"Source SOURCE_ID不在样式")"中,但在进行研究后,未在网上找到与该错误相关的任何内容。

kgsdhlau

kgsdhlau1#

我用最新版本的Mapbox iOS SDK 10.10.0试了一下你的例子,确实FillLayer没有在Map上渲染,我用outdoors样式测试了一下。
问题是,我没有得到你的错误,我不能重现这个错误(也许你使用的是自定义Map样式?):StyleError(rawValue: "Source SOURCE_ID is not in style"),但我通过将源相关数据移动到.mapLoaded事件处理中,成功地在Map上显示了该层:

mapView.mapboxMap.onNext(event: .mapLoaded) { _ in
    let geoJSONDataSourceIdentifier = "SOURCE_ID"
    var geoJSONSource = GeoJSONSource()
    geoJSONSource.data = .url(URL(string: "https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/manhattan.geojson")!)
    try! self.mapView.mapboxMap.style.addSource(geoJSONSource, id: geoJSONDataSourceIdentifier)
    
    var polygonLayer = FillLayer(id: "LAYER_ID")
    polygonLayer.source = "SOURCE_ID"
    polygonLayer.fillColor = .constant(StyleColor(.green))
    polygonLayer.fillOpacity = .constant(1)
    try! self.mapView.mapboxMap.style.addLayer(polygonLayer)
}

希望能有所帮助!

相关问题