问题
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)
- Changing the line width doesn't change it on the screen
func mapView(mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat { return 20.0 }
- How do I change the stroke default color from black to something else?
回答1:
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()
}
回答2:
What @denislexic said, but also, note that you can't change width once the initial width has been set.
来源:https://stackoverflow.com/questions/32024464/customize-mglpolyline-using-mapbox