MyLocationViewController.h
#import
#import "MapKit/MapKit.h"
#import "CoreLocation/CoreLocation.h"
@interface MyLocationViewController : UIViewController
{
MKMapView *userLocationMapView;
}
@property (nonatomic, retain) MKMapView *userLocationMapView;
-(void)moveToUserLocation:(MKUserLocation *)userLocation;
@end
MyLocationViewController.m
#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];
self.userLocationMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
self.userLocationMapView.mapType = MKMapTypeStandard;
userLocationMapView.delegate = self;
userLocationMapView.showsUserLocation = YES;
MKCoordinateRegion mapRegion;
mapRegion.center.latitude = 25.046356; //台北車站
mapRegion.center.longitude = 121.517930; //台北車站
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];
if(userLocationMapView.userLocation != nil)
{
[self moveToUserLocation:userLocationMapView.userLocation];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma - ??
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self moveToUserLocation:userLocation];
}
#pragma mark - Get User Location and Move Map Center
-(void)moveToUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.006;
span.longitudeDelta = 0.006;
region.center = coordinate;
region.span = span;
[userLocationMapView setRegion:region animated:YES];
}
@end
0 意見:
張貼意見