Grouped annotation style
Grouped styles are used for most point and polygon data sources for a weather map. A grouped style categorizes model objects and their annotations based on varying criteria as defined by a series of model evaluator blocks. Then, a separate AWFMapItemStyle
subclass is setup for each category in the group.
These grouped styles are already set up for you by default. However, you can override the styles for one or all categories in a group.
For example, the default style for earthquake point data on a weather map is a grouped style consisting of separate styles for each magnitude category. So first, the individual styles are created for each earthquake magnitude category (identified by AWFEarthquakeAnnotationType
):
var styles = [String: AWFAnnotationStyle]()
// AWFEarthquakeAnnotationStyle conforms to AWFGroupedStyle and contains an `identifiers` method that returns an array
// of category identifiers for the group that we use to iterate over.
AWFEarthquakeAnnotationStyle.identifiers().forEach { (identifier) in
var radius: CGFloat = 8.0
var fillColor = UIColor(red: 0.436, green: 0.702, blue: 0.079, alpha: 1.0)
var label: String?
switch identifier {
case AWFEarthquakeAnnotationType.catastrophic.rawValue:
radius = 23.0
fillColor = UIColor(red: 0.962, green: 0, blue: 1.0, alpha: 1.0)
label = "Catastrophic"
case AWFEarthquakeAnnotationType.great.rawValue:
radius = 18.0
fillColor = UIColor(red: 0.725, green: 0.002, blue: 0.522, alpha: 1.0)
label = "Great"
case AWFEarthquakeAnnotationType.major.rawValue:
radius = 15.0
fillColor = UIColor(red: 0.809, green: 0, blue: 0.323, alpha: 1.0)
label = "Major"
case AWFEarthquakeAnnotationType.strong.rawValue:
radius = 13.0
fillColor = UIColor(red: 0.914, green: 0, blue: 0.020, alpha: 1.0)
label = "Strong"
case AWFEarthquakeAnnotationType.moderate.rawValue:
radius = 11.0
fillColor = UIColor(red: 1.0, green: 0.365, blue: 0, alpha: 1.0)
label = "Moderate"
case AWFEarthquakeAnnotationType.light.rawValue:
radius = 10.0
fillColor = UIColor(red: 0.806, green: 0.561, blue: 0, alpha: 1.0)
label = "Light"
case AWFEarthquakeAnnotationType.minor.rawValue:
radius = 9.0
fillColor = UIColor(red: 0.876, green: 0.798, blue: 0, alpha: 1.0)
label = "Minor"
default:
label = "Mini"
}
let style = AWFEarthquakeAnnotationStyle(radius: radius, fill: fillColor, stroke: .white, strokeWidth: 2.0)
style.identifier = identifier
style.label = label
styles[identifier] = style
}
Then we need to create a grouped style instance that manages these styles, passing in the style and model object types to the class declaration:
let style = AWFGroupedStyle<AWFAnnotationStyle, AWFEarthquake>(styles: styles)
And in order to assign the proper style to each annotation for the data set, we need to setup a series of model evaluator blocks that will return a Boolean value based on whether or not the model belongs to the category:
let evaluators: [String: (AWFEarthquake) -> Bool] = [
AWFEarthquakeAnnotationType.mini.rawValue: { (quake) in
return quake.magnitude < 3.0
},
AWFEarthquakeAnnotationType.minor.rawValue: { (quake) in
return quake.magnitude >= 3.0 && quake.magnitude < 4.0
},
AWFEarthquakeAnnotationType.light.rawValue: { (quake) in
return quake.magnitude >= 4.0 && quake.magnitude < 5.0
},
AWFEarthquakeAnnotationType.moderate.rawValue: { (quake) in
return quake.magnitude >= 5.0 && quake.magnitude < 6.0
},
AWFEarthquakeAnnotationType.strong.rawValue: { (quake) in
return quake.magnitude >= 6.0 && quake.magnitude < 7.0
},
AWFEarthquakeAnnotationType.major.rawValue: { (quake) in
return quake.magnitude >= 7.0 && quake.magnitude < 8.0
},
AWFEarthquakeAnnotationType.great.rawValue: { (quake) in
return quake.magnitude >= 8.0 && quake.magnitude < 9.0
},
AWFEarthquakeAnnotationType.catastrophic.rawValue: { (quake) in
return quake.magnitude >= 9.0
}
]
style.setModelEvaluatorBlocks(evaluators)
Now that your grouped style is setup, you just need to associate it with the earthquake point layer type on your weather map:
weatherMap.style.setStyle(style, forLayerType: .earthquakes)