In this post we will look at the following topics in the MapView framework and using an example.

1.MKMapView

3.MKAnnotation

3.MKPinAnnotationView

MKMapView

This provides you the map you see. On this you can annotate the locations with custom info. Apple does not encourage us to subclass the MapView. Creating MapView is similar to any other view You can use the region property to control the area that is currently visible in the map.

The Map type lets you choose the one of the following types.

  • Standard
  • Satellite
  • Hybrid

MKCoordinateRegion newRegion;

newRegion.center.latitude = 37.786996;

newRegion.center.longitude = -122.440100;

newRegion.span.latitudeDelta = 0.112872;

newRegion.span.longitudeDelta = 0.109863;

[self.mapView setRegion:newRegion animated:YES];

You  can use MKMapViewDelegate protocol to have customize the behavior of the MapView to a certain extent. This gives you a set of optional methods which will get called once Map is loaded, before annotations to the map and also handles the events that happen while the region of the Map gets changed.

MKAnnotation

MKAnnotation can be used to provide annotation related info to the map view.

Custom objects that adopts MKAnnotation Protocol

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface CurrentPositionAnnotation : NSObject {

CLLocationCoordinate2D coordinate;

}

-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinateValue;

@end

The implementation would have the following structure:

#import “CurrentPositionAnnotation.h”

@implementation CurrentPositionAnnotation

@synthesize coordinate;

//Intialize the coordinate value for the annotataion tag

-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinateValue{

coordinate = coordinateValue;

return self;

}

//implementation method is optional, but if you set the canShowCallout” property to YES in Annotation view, this is required

//string is displayed in the callout for the associated annotation view

– (NSString *)title

{

return @”Current Location”;

}

// optional

– (NSString *)subtitle

{

return @”Your subtitle”;

}@end

Finally, adding annotation to your mapview:

CurrentPositionAnnotation *annotation = [[CurrentPositionAnnotation alloc] initWithCoordinate:somelocation];

[mapView addAnnotation:annotation];

[annotation release];

MKPinAnnotationView

This is a concrete annotation view and  displays a pin in the location you specify. The pin color , Animation of the Pin view (dropping from the top) are configurable.

pinView.pinColor = MKPinAnnotationColorPurple;

pinView.animatesDrop = YES;

pinView.canShowCallout = YES;

Drop a line in the comments section, if you find it useful or you have any other queries.

Advertisements