1 / 16

Overlay on a Map

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

ricky
Download Presentation

Overlay on a Map

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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)?

  3. 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

  4. 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)

  5. MKMapView class MKCircle *circle = [MKCircle circleWithCenterCoordinate: whiteHouse.coordinate radius: 200]; [map addOverlay: circle];

  6. 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

  7. 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>)

  8. 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

  9. 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

  10. MKMapView class @interface OurViewController : UIViewController <MKMapViewDelegate> { IBOutlet MKMapView *map; }

  11. 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;

  12. 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)

  13. mapView:rendererForOverlay: method • (MKOverlayRenderer *) mapView: (MKMapView *) mapView rendererForOverlay:(id) <MKOverlay>) overlay • Inside that method, build a MKOverlayRenderer object and return it

  14. 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

  15. mapView:rendererForOverlay: method • The overlay parameter is the MKCircle passed to addOverlay earlier •  check it out using NSLog MKCircleRenderer *circleRenderer = [[MKCircleRenderer alloc] initWithOverlay: overlay];

  16. mapView:rendererForOverlay: method • Set stroke and fill colors circleRenderer.strokeColor = [UIColor redColor]; circleRenderer.fillColor = [[UIColor redColor] colorWithAlphaComponent: 0.5]; return circleRenderer;

More Related