【问题标题】:Change color of polyline in static map image更改静态地图图像中折线的颜色
【发布时间】:2020-11-11 20:04:56
【问题描述】:

我正在使用 MapboxStatic 成功创建静态图像,并将记录的轨道坐标作为 GeoJson 数据。

我不知道如何编辑绘制在图像上的路线的颜色 - 它默认为黑色/灰色,我找不到更改它的方法。

我意识到这必须是通过编辑 GeoJson 属性本身,但我不知道如何快速编辑它?

很想了解如何以某种方式更改颜色...

       let camera = SnapshotCamera(
            lookingAtCenter: CLLocationCoordinate2D(latitude: self.mapView.camera.centerCoordinate.latitude, longitude: self.mapView.camera.centerCoordinate.longitude),
            zoomLevel: 12)
        let options = SnapshotOptions(
            styleURL: self.mapView.styleURL,
            camera: camera,
            size: self.mapView.bounds.size)
        
        let coordinates = self.viewModel.getTrack().locations?.map { loc -> CLLocationCoordinate2D in
            CLLocationCoordinate2D(latitude: loc.latitude, longitude: loc.longitude)
        }
        
        let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))

        let data = trackLine.geoJSONData(usingEncoding: String.Encoding.utf8.rawValue)
        let getJson = GeoJSON(objectString: String(data: data, encoding: .utf8)!)
       
        options.overlays.append(getJson)

【问题讨论】:

  • 我使用的是 MapBox 而不是 MapKit
  • 感谢@AnatolySukhanov,但事实并非如此-您链接的内容是关于将其设置在对我来说很好的实时地图上。我说的是拍摄地图的快照以创建图像。它以黑色绘制路线,我不知道如何更改它
  • 抱歉,我是新手,我花了一些时间才弄明白。看我的回答。

标签: swift mapbox mapbox-ios mapbox-static-maps


【解决方案1】:

您需要使用MGLPolylineFeature 来控制特征属性。

所以不是

let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))

它应该看起来像

let trackLine = MGLPolylineFeature(coordinates: coordinates!, count: UInt(coordinates!.count))
        
polyline.attributes.updateValue("red", forKey: "stroke")

【讨论】:

  • 哇@Anatoly Sukhanov 非常感谢你,它工作得很好!老实说,我不认为我会在这里找到答案 - 你是个好伙伴!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多