如何更改MGLPolyline的颜色?

时间:2019-02-07 05:28:29

标签: ios swift mglmapview

如何更改MGLPolyline的颜色?我看过here,但答案没有用。我也尝试了其他一些代码,但是它过于依赖苹果地图。那么,如何更改线条的颜色,并且最好能够同时编辑线条的其他方面?贝娄是我认为应该在代码中进行这些编辑的地方,但我不是100%。

        @objc func useButtonTap(_ sender: UIGestureRecognizer) {
        print("use tap button pressed")
        startButton.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.9)
        useTrackButton.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.9)

        locationManager?.stopUpdatingLocation()

        print(locationList, "<-- location manager array in use button")
        if locationList == [] {
            print("nothing was tracked")
        } else {

            var mapCoordinates: [CLLocationCoordinate2D] = []

            for locationT in locationList {
                print(locationT, "<-- locationT")
                print(locationT.coordinate.latitude, "<-- latitude")
                print(locationT.coordinate.longitude, "<-- longitude")

                let locationLatitude = locationT.coordinate.latitude
                let locationLongitude = locationT.coordinate.longitude

                print(locationLatitude, "lat")
                print(locationLongitude, " long")
                let newCoord = CLLocationCoordinate2D(latitude: locationLatitude, longitude: locationLongitude) // - (0.0001/2)
                print(newCoord, "<-- new coord")
                mapCoordinates.append(newCoord)//+ 0.0001

            }

            polyline = MGLPolyline(coordinates: mapCoordinates, count: UInt(mapCoordinates.count))
//            toCome = polyline
//            mapView.add(polyline)

            mapView.addAnnotation(polyline) //used to be jsut .add, changed in order to try anwser for color chaning of polyline

        }
    }

3 个答案:

答案 0 :(得分:0)

如果您阅读了手册,可以看到fillColor和strokeColor方法...

//Circle overlay
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay is MKCircle {

        let circle = MKCircleRenderer(overlay: overlay)
        circle.strokeColor = .blue
        circle.fillColor = .red
        circle.lineWidth = 3
        return circle
    } else {
        return MKOverlayRenderer()
    }
}

因此,您应该查找overlayrenderer文档。

答案 1 :(得分:0)

您只能在 MKMapViewDelegate 函数中设置折线的颜色和宽度。查看下面的示例。

  public func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
      let polyLine = overlay as? MKPolyline
      let renderer = MKPolylineRenderer(overlay: overlay)
      renderer.lineWidth = 2.5
      renderer.strokeColor = UIColor(red: 47/255, green: 62/255, blue: 158/255, alpha: 1.0)
      return renderer
  }

答案 2 :(得分:0)

您必须制作mapView.delegate = self

然后实现此方法:

func mapView(_ mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {

  return .blue
}