使用 mapbox 自定义 MGLPolyline

Customize MGLPolyline using mapbox

刚开始使用 Mapbox,通过在 locationManaged didUpdateLocations 中添加它设法绘制了 MGLPolyline

   var shape = MGLPolyline(coordinates: &a, count: UInt(a.count))
   mapView.addAnnotation(shape) 
  1. 更改线宽不会在屏幕上改变它

func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat { return 20.0 }

  1. 如何将描边默认颜色从黑色更改为其他颜色?

您需要将地图委托设置为 self 才能使函数正常工作。这是代码:

MGLMapViewDelegate

开始你的viewController
class yourController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, MGLMapViewDelegate{

然后在设置好地图后,像这样添加self.mapView.delegate = self

mapView = MGLMapView(frame: mapViewWrapper.bounds, styleURL: NSURL(string: Mapbox.getTheme()))
mapView = Mapbox.configure(mapView)
mapView.setCenterCoordinate(appleMap.userLocation.coordinate, zoomLevel: 12, animated: true)
mapViewWrapper.addSubview(mapView)
self.mapView.delegate = self

那么你的功能将起作用:

func mapView(mapView: MGLMapView, alphaForShapeAnnotation annotation: MGLShape) -> CGFloat {
   // Set the alpha for all shape annotations to 1 (full opacity)
   return 1
}

func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat {
   // Set the line width for polyline annotations
   return 5.0
}

func mapView(mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
   // Give our polyline a unique color by checking for its `title` property
   return UIColor.redColor()
}

@denislexic 所说的,但还要注意,一旦设置了初始宽度,您就无法更改宽度。