使用 mapbox 自定义 MGLPolyline

den*_*xic 1 mapkit mapbox swift

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

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

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

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

den*_*xic 5

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

启动你的viewController MGLMapViewDelegate

class yourController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, MGLMapViewDelegate{
Run Code Online (Sandbox Code Playgroud)

然后在设置地图后,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
Run Code Online (Sandbox Code Playgroud)

然后您的功能将起作用:

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()
}
Run Code Online (Sandbox Code Playgroud)