1

Just started using Mapbox, managed to draw a MGLPolyline by adding this in the locationManaged didUpdateLocations

   var shape = MGLPolyline(coordinates: &a, count: UInt(a.count))
   mapView.addAnnotation(shape) 
  1. Changing the line width doesn't change it on the screen

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

  1. How do I change the stroke default color from black to something else?
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
denislexic
  • 10,786
  • 23
  • 84
  • 128

2 Answers2

5

You need to set the map delegate to self for the functions to work. Here is the code:

Initiate your viewController with MGLMapViewDelegate

class yourController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, MGLMapViewDelegate{

Then after you set the map, add self.mapView.delegate = self like so

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

Then your functions will work:

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
  • 10,786
  • 23
  • 84
  • 128
  • Any idea how to show poly line in 3d format like how MapBox mentioned in their blog video here https://blog.mapbox.com/3d-map-visualizations-on-android-and-ios-81461cd384f8 ? – AskIOS Jul 06 '18 at 09:13
1

What @denislexic said, but also, note that you can't change width once the initial width has been set.

incanus
  • 5,100
  • 1
  • 13
  • 20