Adding custom map annotations
Oftentimes you'll want to display your custom data on the map managed by AWFWeatherMap, such as custom annotations and/or overlays. However, since your AWFWeatherMap instance must remain the map view's delegate to retain the built-in weather layer functionality, you cannot change the map view's delegate
.
Fortunately, all delegate methods associated with your chosen mapping strategy (e.g. Apple Maps/MapKit, Google Maps or Mapbox) will get forwarded to the delegate you assign on AWFWeatherMap's mapViewDelegate
property. This allows you to use the map view managed by your AWFWeatherMap instance as you typically would when interacting with the native map view instance directly outside of our SDK.
To add custom annotations to your AWFWeatherMap instance, just set the mapViewDelegate
property and add your annotations by accessing the map strategy and implementing the necessary map delegate methods as required by your chosen mapping library (e.g. MKMapViewDelegate
when using the default of Apple Maps):
import UIKit
import AerisWeatherKit
import AerisMapKit
class CustomAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
init(coordinate coord: CLLocationCoordinate2D) {
self.coordinate = coord
}
}
class CustomMapViewController: AWFWeatherMapViewController {
override func viewDidLoad() {
super.viewDidLoad()
// our controller will receive MKMapViewDelegate events
weatherMap.mapViewDelegate = self
// add custom annotation
let annotation = CustomAnnotation(coordinate: CLLocationCoordinate2D(latitude: 47.5, longitude: -122.5))
weatherMap.strategy.addAnnotation(annotation)
}
}
extension CustomMapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView: MKAnnotationView?
// handle our custom annotation view by checking the instance type
if let annotation = annotation as? CustomAnnotation {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "CustomAnnotationView")
} else if let strategy = weatherMap.strategy as? AWFAppleMapStrategy {
// fall back to the built-in AWFWeatherMap functionality for the annotation
annotationView = strategy.defaultAnnotationView(forAnnotation: annotation)
}
return annotationView
}
}