Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

iOS Map Development

IOS Map Development


Introduction

IOS Maps help us locate positions; IOS Maps use the MapKit framework.

Example Steps

1. Create a simple View based application

2. Select the project file, then the target, then add MapKit.framework.

3. Add Corelocation.framework

4. Add a map view to ViewController.xib and create an ibOutlet named mapView.

5. Create a new file by selecting Objective C class via File-> New -> File... -> and click Next

6. Set 'sub class of' to NSObject and name the class MapAnnotation

7. Select Create

8. Update MapAnnotation.h as shown below



#import 

#import 



@interface MapAnnotation : NSObject

@property (nonatomic, strong) NSString *title;

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;



- (id)initWithTitle:(NSString *)title andCoordinate:

  (CLLocationCoordinate2D)coordinate2d;



@end

9. Update MapAnnotation.m as shown below



#import "MapAnnotation.h"



@implementation MapAnnotation

-(id)initWithTitle:(NSString *)title andCoordinate:

 (CLLocationCoordinate2D)coordinate2d{    

    self.title = title;

    self.coordinate =coordinate2d;

    return self;

}

@end

10. Update ViewController.h as follows



#import 

#import 

#import 

@interface ViewController : UIViewController

{

    MKMapView *mapView;

}

@end

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  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 you run the application, the output is as follows

mapOutput1

When we scroll the map upward, the output is as follows

mapOutput2