Description
Develop
Map application to display map location based on the specified location.
Project Creation
- Create new Xcode project and select “Single View project” template under iOS->Application
- Add CoreLocation and MapKit Frameworks, which are necessary to handle Core location and Map functionalities.
- Open ViewController.xib file and add MapView UI object from the object library. Connect mapView object with the File Owner. The mapView is defined in the ViewController.h interface
Do method implementation
1) ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
IBOutlet MKMapView *mapView;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end
2) ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize locationManager, mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled]) {
locationManager.delegate = self;
[locationManager startUpdatingLocation];
} else {
NSLog(@"Location Service
exception...");
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"latitude=%f,
longitude=%f",
[newLocation
coordinate].latitude,
[newLocation
coordinate].longitude);
MKCoordinateRegion region = MKCoordinateRegionMake([newLocation coordinate], MKCoordinateSpanMake(.2, .2));
[mapView setCenterCoordinate:[newLocation coordinate]];
[mapView setRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"Error");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources
that can be recreated.
}
- (void)dealloc {
[locationManager release];
[super dealloc];
}
@end
Testing










