maps are always helpful for us to locate places. maps are integrated in ios using the mapkit framework.
steps involved
step 1 − create a simple view-based application.
step 2 − select your project file, then select targets and then add mapkit.framework.
step 3 − we should also add corelocation.framework.
step 4 − add a mapview to viewcontroller.xib and create an iboutlet and name it as mapview.
step 5 − create a new file by selecting file → new → file... → select objective c class and click next.
step 6 − name the class as mapannotation with "sub class of" as nsobject.
step 7 − select create.
step 8 − update mapannotation.h as follows −
#import <foundation/foundation.h> #import <mapkit/mapkit.h> @interface mapannotation : nsobject<mkannotation> @property (nonatomic, strong) nsstring *title; @property (nonatomic, readwrite) cllocationcoordinate2d coordinate; - (id)initwithtitle:(nsstring *)title andcoordinate: (cllocationcoordinate2d)coordinate2d; @end
step 9 − update mapannotation.m as follows −
#import "mapannotation.h"
@implementation mapannotation
-(id)initwithtitle:(nsstring *)title andcoordinate:
(cllocationcoordinate2d)coordinate2d {
self.title = title;
self.coordinate =coordinate2d;
return self;
}
@end
step 10 − update viewcontroller.h as follows −
#import <uikit/uikit.h>
#import <mapkit/mapkit.h>
#import <corelocation/corelocation.h>
@interface viewcontroller : uiviewcontroller<mkmapviewdelegate> {
mkmapview *mapview;
}
@end
step 11 − update viewcontroller.m as follows −
#import "viewcontroller.h"
#import "mapannotation.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
mapview = [[mkmapview alloc]initwithframe:
cgrectmake(10, 100, 300, 300)];
mapview.delegate = self;
mapview.centercoordinate = cllocationcoordinate2dmake(37.32, -122.03);
mapview.maptype = mkmaptypehybrid;
cllocationcoordinate2d location;
location.latitude = (double) 37.332768;
location.longitude = (double) -122.030039;
// add the annotation to our map view
mapannotation *newannotation = [[mapannotation alloc]
initwithtitle:@"apple head quaters" andcoordinate:location];
[mapview addannotation:newannotation];
cllocationcoordinate2d location2;
location2.latitude = (double) 37.35239;
location2.longitude = (double) -122.025919;
mapannotation *newannotation2 = [[mapannotation alloc]
initwithtitle:@"test annotation" andcoordinate:location2];
[mapview addannotation:newannotation2];
[self.view addsubview:mapview];
}
// when a map annotation point is added, zoom to it (1500 range)
- (void)mapview:(mkmapview *)mv didaddannotationviews:(nsarray *)views {
mkannotationview *annotationview = [views objectatindex:0];
id <mkannotation> mp = [annotationview annotation];
mkcoordinateregion region = mkcoordinateregionmakewithdistance
([mp coordinate], 1500, 1500);
[mv setregion:region animated:yes];
[mv selectannotation:mp animated:yes];
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
@end
output
when we run the application, we'll get the output as shown below −
when we scroll the map up, we will get the output as shown below −