I am trying to add Annotations and MGLineStyleLayer to the mapView. I am successfully adding them but the LineLayer is drawn above the annotations. I want the annotations to be drawn over the line layers. Here is my implementation
/*This is where i am adding the markers*/
func createMapPoints(points: [CLLocationCoordinate2D], dashed: Bool = false) {
var waypointType: MapAnnotationEnum = .waypointAnnotation
if dashed {
waypointType = .offsetWaypoint
}
addLineLabel(points: points, width: 7, color: UIColor.white, dashed: dashed)
for point in points {
_ = addAnnotationLabel(location: point, title: "WP", type: waypointType)
}
}
func addAnnotationLabel(location: CLLocationCoordinate2D, title: String, type: MapAnnotationEnum) -> CustomMapGLAnnotaion {
let annotation = CustomMapGLAnnotaion()
annotation.coordinate = location
annotation.title = title
annotation.annotationType = type
self.mapView.addAnnotation(annotation)
return annotation
}
func addLineLabel(points: [CLLocationCoordinate2D], width: CGFloat, color: UIColor, dashed: Bool = false) {
let polyline = CustomMapGLPolyline(coordinates: points, count: UInt(points.count))
polyline.width = width
polyline.color = color
shapeCount += 1
if dashed {
polyline.title = "dashed"
addDashedLine(polyline: polyline)
} else {
polyline.title = "0"
addCasingLine(polyline: polyline)
}
print("Last Execution Point 3")
self.mapView.addAnnotation(polyline)
}
func addCasingLine(polyline: MGLPolyline) {
guard let style = self.mapView.style else { return }
let source = MGLShapeSource(identifier: "line\(shapeCount)", shape: polyline, options: nil)
style.addSource(source)
style.layer(withIdentifier: )
let lineLayer = MGLLineStyleLayer(identifier: "line-layer\(shapeCount)", source: source)
lineLayer.lineJoin = NSExpression(forConstantValue: "round")
lineLayer.lineCap = NSExpression(forConstantValue: "round")
lineLayer.lineColor = NSExpression(forConstantValue: #colorLiteral(red: 0.1254901961, green: 0.4901960784, blue: 0.9137254902, alpha: 1))
lineLayer.lineOpacity = NSExpression(forConstantValue: 1)
lineLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [14: 4, 18: 4])
style.addLayer(lineLayer)
}
func addDashedLine(polyline: MGLPolyline) {
guard let style = self.mapView.style else { return }
let source = MGLShapeSource(identifier: "line\(shapeCount)", shape: polyline, options: nil)
style.addSource(source)
let dashedLayer = MGLLineStyleLayer(identifier: "polyline-dash\(shapeCount)", source: source)
dashedLayer.lineJoin = NSExpression(forConstantValue: "round")
dashedLayer.lineCap = NSExpression(forConstantValue: "round")
dashedLayer.lineColor = NSExpression(forConstantValue: #colorLiteral(red: 0.9411764706, green: 0.3764705882, blue: 0.1921568627, alpha: 1))
dashedLayer.lineOpacity = NSExpression(forConstantValue: 1)
dashedLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", [14: 4, 18: 4])
dashedLayer.lineDashPattern = NSExpression(forConstantValue: [0, 1.5])
style.addLayer(dashedLayer)
}
This is the definition of the two custom classes
class CustomMapGLAnnotaion: MGLPointAnnotation {
var annotationType: MapAnnotationEnum?
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class CustomMapGLPolyline: MGLPolyline {
var width: CGFloat?
var color: UIColor?
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Where am i going wrong? Thanks for helping!
For someone looking for an answer in the future!
Since
MGLAnnotationViewinherits from aUIViewobject. We can decide the ordering of the annotation feature simply by setting itszIndexproperty.According to https://docs.mapbox.com/ios/maps/overview/markers-and-annotations/