1)Create new Project TestMkMapkit (Navigation based) in Xcode

Open the RootViewController.xib file in Interface Builder, remove default UITableView tool from xib file and add MapView in RootViewcontroller.xib.

2)Add Mapkit framework in project

In Groups and Files,right click on Framework, add->Existing frameworks->Select MapKit framework to access MKMapkit class and MKAnnotation class.

3)Set RootViewController.h file

Set .h Root file as UIViewController’s subclass and add MKMapViewDelegate in it, create IBOutlet object for a Mapkit

4)Used Interface Builder to connect UI control to code

and synthesize the mapView Object in .m class like
@synthesize mapView;

5)Build and run the program to see the successful access of google maps in iPhone using Mapkit framework

6)Create your separate MyAnnotation class to show multiple annotations on Map

Basically MyAnnotation class is a subclass of MkAnnotation having three major attributes-

CLLocationCoordinate2D- to set the latitude and longitude position
Title- to set the title of Annotation point
subTitle – we can set subtitle to MKAnnotationPoint

MyAnnotation.h

MyAnnotation.m

now import MyAnnotation.h file in RootViewController.m file, like this- #import “MyAnnotation.h” to create multiple Annotations.

7)If you want to access current location of user using CLLocation class

write following code in RootViewcontroller.m class

- (void)viewDidLoad {
    [super viewDidLoad];	
	CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;	
	NSLog(@"user latitude = %f",userCoordinate.latitude);
	NSLog(@"user longitude = %f",userCoordinate.longitude);	
	mapView.delegate=self;
}

8)Now create multiple annotations and add all annotation points in one mutable array.

Append this code at the end of viewDidLoad method,
 
NSMutableArray* annotations=[[NSMutableArray alloc] init];
	
	CLLocationCoordinate2D theCoordinate1;
    theCoordinate1.latitude = 37.786996;
    theCoordinate1.longitude = -122.419281;
	
	CLLocationCoordinate2D theCoordinate2;
    theCoordinate2.latitude = 37.810000;
    theCoordinate2.longitude = -122.477989;
	
	CLLocationCoordinate2D theCoordinate3;
    theCoordinate3.latitude = 37.760000;
    theCoordinate3.longitude = -122.447989;
	
	CLLocationCoordinate2D theCoordinate4;
    theCoordinate4.latitude = 37.80000;
    theCoordinate4.longitude = -122.407989;
	
	MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
	
	myAnnotation1.coordinate=theCoordinate1;
	myAnnotation1.title=@"Rohan";
	myAnnotation1.subtitle=@"in the city";
	
	MyAnnotation* myAnnotation2=[[MyAnnotation alloc] init];
	
	myAnnotation2.coordinate=theCoordinate2;
	myAnnotation2.title=@"Vaibhav";
	myAnnotation2.subtitle=@"on a Bridge";
	
	MyAnnotation* myAnnotation3=[[MyAnnotation alloc] init];
	
	myAnnotation3.coordinate=theCoordinate3;
	myAnnotation3.title=@"Rituraj";
	myAnnotation3.subtitle=@"in the forest";
	
	MyAnnotation* myAnnotation4=[[MyAnnotation alloc] init];
	
	myAnnotation4.coordinate=theCoordinate4;
	myAnnotation4.title=@"Sahil";
	myAnnotation4.subtitle=@"at Russian Hill";
	
	[mapView addAnnotation:myAnnotation1];
	[mapView addAnnotation:myAnnotation2];
	[mapView addAnnotation:myAnnotation3];
	[mapView addAnnotation:myAnnotation4];
	
	[annotations addObject:myAnnotation1];
	[annotations addObject:myAnnotation2];
	[annotations addObject:myAnnotation3];
	[annotations addObject:myAnnotation4];

        NSLog(@"%d",[annotations count]);

we added all the annotations on Map as well as in the mutable array, to Position the map so that all overlays and annotations are visible on screen,
by using annotations(MKMutableArray class) array, we gone integrate all the MK points(one by one) with the MKMapRect class, to concentrate the map with the all the MK points.

Append this code at the end of viewDidLoad method to concentratet the Map
// Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
	for (id  annotation in annotations) {
		NSLog(@"fly to on");
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) {
            flyTo = pointRect;
        } else {
            flyTo = MKMapRectUnion(flyTo, pointRect);
        }
    }
    
    // Position the map so that all overlays and annotations are visible on screen.
    mapView.visibleMapRect = flyTo;

9)Now Once again Build and Run the project to see the all created Annotations on a Screen.

here is the image which shows all the points with respect to their latitude and longitude positions.

10)Last but not least. To set the MKAnnotation’s delegate method.

write the following code in RootViewcontroller.m class to play with the MKAnnotation’s delegate,

above code shows, how to animate MK points, how to change the color of MKAnnotation Points, how to set leftCalloutAccessoryView, rightCalloutAccessoryView of MKPinAnnotationView class.

11)Build and Run now to see the delegate method’s effects on MKPinAnnotations

that’s it from me, hopefully this will help to all iphone beginners!

code link-here

About these ads