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
}
}
#pragma mark - CustomAnnotation
@interface CustomAnnotation : NSObject<MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coord;
@end
@implementation CustomAnnotation
@synthesize coordinate;
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
self.coordinate = coord;
}
return self;
}
@end
#pragma mark - CustomMapViewController
@interface CustomMapViewController : AWFWeatherMapViewController <MKMapViewDelegate>
@end
@implementation CustomMapViewController
- (void)viewDidLoad {
[super viewDidLoad];
// our controller will receive MKMapViewDelegate events
self.weatherMap.mapViewDelegate = self;
// add custom annotation
CustomAnnotation *annotation = [CustomAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(47.5, -121.5)];
[(MKMapView *)self.weatherMap.mapView addAnnotation:annotation];
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = nil;
// handle our custom annotation view by checking the instance type
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"];
} else {
// fall back to the built-in AWFWeatherMap functionality for the annotation
annotationView = [self.weatherMap.strategy defaultAnnotationViewForAnnotation:annotation];
}
return annotationView;
}
@end
Last modified: May 21, 2020