2011年10月21日

在MapView上面顯示User Location (法一)

這是完全不考慮執行效果、也沒有把method好好切的寫法,只有先求能把使用者位置在地圖上標示出來。

MyLocationViewController.h

#import
#import "MapKit/MapKit.h"
#import "CoreLocation/CoreLocation.h"

@interface MyLocationViewController : UIViewController
{
MKMapView *userLocationMapView;
CLLocationManager *locationManager;
}

@property (nonatomic, retain) MKMapView *userLocationMapView;

@end


MyLocationViewController.h


#import "MyLocationViewController.h"

@implementation MyLocationViewController
@synthesize userLocationMapView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

-(void)dealloc
{
[userLocationMapView release];
[super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
self.userLocationMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
userLocationMapView.delegate = self;
userLocationMapView.showsUserLocation = YES;
self.userLocationMapView.mapType = MKMapTypeStandard;

CLLocationCoordinate2D coordinate = locationManager.location.coordinate;

MKCoordinateRegion mapRegion;
mapRegion.center = coordinate;
MKCoordinateSpan mapSpan;
mapSpan.latitudeDelta = 0.006;
mapSpan.longitudeDelta = 0.006;
mapRegion.span = mapSpan;
[userLocationMapView setRegion:mapRegion animated:YES];
self.userLocationMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.userLocationMapView];
}

- (void)viewDidUnload
{
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

0 意見:

張貼意見