
//FourthViewController.h
#import
#import "MapKit/Mapkit.h"
#import "Annotation.h"
@interface FourthViewController : UIViewController
{
MKMapView *ntuMapView;
}
@property (nonatomic, retain) MKMapView *ntuMapView;
@end
//FourthViewController.m
#import "FourthViewController.h"
@implementation FourthViewController
@synthesize ntuMapView;
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:view.annotation.title
message:view.annotation.subtitle
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)dealloc
{
[ntuMapView release];
[super dealloc];
}
- (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
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *tempmapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
self.ntuMapView = tempmapView;
tempmapView.delegate = self;
self.ntuMapView.mapType = MKMapTypeStandard;
MKCoordinateRegion mapRegion;
mapRegion.center.latitude = 25.0157701;
mapRegion.center.longitude = 121.533397;
MKCoordinateSpan span;
span.latitudeDelta = 0.006;
span.longitudeDelta = 0.006;
mapRegion.span = span;
[ntuMapView setRegion:mapRegion animated:YES];
self.ntuMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.ntuMapView];
[tempmapView release];
CLLocationCoordinate2D location0 = {25.0157701,121.533397};
Annotation *myAnnotation0 = [[Annotation alloc] initWhithTitle:@"台大"
subTitle:@"hahaha"
andCoordiante:location0];
CLLocationCoordinate2D location1 = {25.016784,121.53132};
Annotation *myAnnotation1 = [[Annotation alloc] initWhithTitle:@"公館夜市"
subTitle:@"好好吃"
andCoordiante:location1];
[ntuMapView addAnnotations:[NSArray arrayWithObjects:myAnnotation0, myAnnotation1, nil]];
[myAnnotation0 release];
[myAnnotation1 release];
}
- (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);
}
#pragma mark methods for MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)map
viewForAnnotation:(id
{
static NSString *AnnotationViewID = @"annotationViewID";
MKPinAnnotationView* pinView =
(MKPinAnnotationView *)[ntuMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (!pinView)
{
MKPinAnnotationView* customPinView =
[[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationViewID]
autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout =YES;
customPinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
@end
//Annotation.h
#import
#import
@interface Annotation : NSObject
{
NSString *_title;
NSString *_subTitle;
CLLocationCoordinate2D _coordiante2D;
}
@property (nonatomic , readonly) NSString *title;
@property (nonatomic , readonly) NSString *subtitle;
@property (nonatomic , readonly) CLLocationCoordinate2D coordinate;
-(id) initWhithTitle:(NSString *) theTitle subTitle:(NSString *)theSubTitle andCoordiante:(CLLocationCoordinate2D) theCoordinate;
@end
//Annotation.m
#import "Annotation.h"
@implementation Annotation
@synthesize title=_title;
@synthesize subtitle=_subTitle;
@synthesize coordinate=_coordiante2D;
-(id) initWhithTitle:(NSString *) theTitle subTitle:(NSString *)theSubTitle andCoordiante:(CLLocationCoordinate2D) theCoordinate
{
self = [super init];
if (self) {
_title = [theTitle copy];
_subTitle = [theSubTitle copy];
_coordiante2D = theCoordinate;
}
return self;
}
- (void)dealloc
{
[_title release];
[_subTitle release];
[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
0 意見:
張貼意見