用4.2创建的空模班,添加的UIViewController类,取名为mapView.(按正规命名方式应该为MapView,大家见谅)。
以下为添加的代码
mapView.h
#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> #import <QuartzCore/QuartzCore.h> @interface mapView : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locManager; MKMapView *map; CLLocation *bestLocation; } @property (retain) CLLocationManager *locManager; @property (retain) CLLocation *bestLocation; @end
mapView.m
//
// mapView.m
// Map
//
// Created by SuperHappy on 12-1-31.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "mapView.h"
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
@implementation mapView
@synthesize locManager;
@synthesize bestLocation;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Location manager error :%@",[error description]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (!self.bestLocation) {
self.bestLocation = newLocation;
}
else if(newLocation.horizontalAccuracy < bestLocation.horizontalAccuracy)
self.bestLocation = newLocation;
map.region = MKCoordinateRegionMake(self.bestLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f));
map.showsUserLocation = YES;
map.zoomEnabled = NO;
}
- (void)findme
{
// disable right button
self.navigationItem.rightBarButtonItem = nil;
// Search for the best location
self.bestLocation = nil;
self.locManager.delegate = self;
[self.locManager startUpdatingLocation];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
map = [[MKMapView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:map];
[map release];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
self.locManager = [[[CLLocationManager alloc] init] autorelease];
if (!self.locManager.locationServicesEnabled)
{
NSLog(@"User has opted out of location services");
return;
}
else
{
// User generally allows location calls
self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Find Me", @selector(findme));
}
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end