160 likes | 379 Views
Overlay on a Map. Overlays can be added to a map to highlight an area of interest or a route, .. Can be standard or user-defined We will focus on standard overlays (circles, polygons). Overlay on a Map. To add an overlay to a map (a MKMapView object), we call the method addOverlay
E N D
Overlay on a Map • Overlays can be added to a map to highlight an area of interest or a route, .. • Can be standard or user-defined • We will focus on standard overlays (circles, polygons)
Overlay on a Map • To add an overlay to a map (a MKMapView object), we call the method addOverlay • -(void) addOverlay: (id <MKOverlay>) overlay; • what does (id <MKOverlay>) mean (that is the data type of the parameter)?
Overlay on a Map • what does (id <MKOverlay>) mean? • It means an object that conforms to the MKOverlay protocol • MKCircle, MKPolygon are classes that conform to the MKOverlay protocol
Overlay on a Map • We will use MKCircle to add a circle overlay to our map • Use circleWithCenterCoordinate:radius: method to create a MKCircle object • Takes 2 parameters: a CLLocationCoordinate2D (for the center of the circle) and a CLLocationDistance (for the radius of the circle, in meters)
MKMapView class MKCircle *circle = [MKCircle circleWithCenterCoordinate: whiteHouse.coordinate radius: 200]; [map addOverlay: circle];
MKMapView class • MKMapView has a delegate, of type (id <MKMapViewDelegate>), i.e. an object implementing the MKMapViewDelegate protocol • A map view object sends messages to its delegate regarding the loading of the map data and changes in the portion of the map being displayed; the delegate also manages annotation views to highlight points of interest on the map
Overlay on a Map • addOverlay automatically calls the method • (MKOverlayRenderer *) mapView: (MKMapView *) mapView rendererForOverlay:(id) <MKOverlay>) overlay • of the delegate of the MKMapView, which is of type ( id <MKMapViewDelegate>)
Overlay on a Map • That method, mapView:rendererForOverlay:, is the method doing the work to add the overlay • we need to code in a class that implements the MKMapViewDelegate protocol • And set the delegate of the MKMapView to be an object of that class
Overlay on a Map • The easiest is for our ViewController class to implement the MKMapViewDelegate protocol, set the delegate of the MKMapView to self, and implement the mapView:rendererForOverlay: inside our ViewController class
MKMapView class @interface OurViewController : UIViewController <MKMapViewDelegate> { IBOutlet MKMapView *map; }
MKMapView class • self is an object that conforms to the MKMapViewDelegate protocol • self can be the delegate of map • In the .m file, we will set that up map.delegate = self;
MKMapView class • Now we can override the mapView: rendererForOverlay: method inside our ViewController class • .. which is the method automatically called when we call addOverlay with a MKMapView object (map in this case)
mapView:rendererForOverlay: method • (MKOverlayRenderer *) mapView: (MKMapView *) mapView rendererForOverlay:(id) <MKOverlay>) overlay • Inside that method, build a MKOverlayRenderer object and return it
MKCircleRenderer class • The MKCircleRenderer class, which inherits from MKOverlayRenderer a MKCircleRenderer object “is a” MKOverlayRenderer object • It provides a visual representation for a MKCircle overlay object
mapView:rendererForOverlay: method • The overlay parameter is the MKCircle passed to addOverlay earlier • check it out using NSLog MKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] initWithOverlay: overlay];
mapView:rendererForOverlay: method • Set stroke and fill colors circleRenderer.strokeColor = [UIColor redColor]; circleRenderer.fillColor = [[UIColor redColor] colorWithAlphaComponent: 0.5]; return circleRenderer;