2010/06/14

[iPhone][Objective-C]ピンとannotationを表示

今日は、地図上にピンとannotationを表示するプログラムを掲載します。
//MapTestAppDelegate.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapViewController.h"
#import "MapView.h"
#import "AnnotationToMap.h"
#import "MapViewDelegate.h"

@interface MapTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

//MapTestAppDelegate.m
#import "MapTestAppDelegate.h"

@implementation MapTestAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:
(UIApplication *)application {

CLLocation *location =
[[CLLocation alloc] initWithLatitude:35.660262 longitude:139.729548];
CLLocationCoordinate2D mapcenter = location.coordinate;
[location release];

MKMapView *mapview =
[[MKMapView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
MKCoordinateSpan CoordinateSpan = MKCoordinateSpanMake(0.005,0.005);
MKCoordinateRegion CoordinateRegion =
MKCoordinateRegionMake(mapcenter,CoordinateSpan);
[mapview setRegion:CoordinateRegion animated:YES];

MapViewDelegate *mapviewdelegate = [[MapViewDelegate alloc] init];
mapview.delegate = mapviewdelegate;

AnnotationToMap *annotationtomap =
[[AnnotationToMap alloc] initWithCoordinate:mapcenter];

[mapview addAnnotation:annotationtomap];
[annotationtomap release];

MapViewController *mapviewcontroller =
[[MapViewController alloc] initWithNibName:nil bundle:nil];
mapviewcontroller.view = mapview;

[window addSubview:mapviewcontroller.view];
[mapview release];
[window makeKeyAndVisible];

}


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


@end

//MapViewController.h
#import <UIKit/UIKit.h>

@interface MapViewController : UIViewController {

}


@end

//MapViewController.m
#import "MapViewController.h"


@implementation MapViewController


- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
if (self =
[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

}
return self;
}


- (void)loadView {
}

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

- (void)viewDidUnload {
}


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


@end

//MapView.h
#import <MapKit/MapKit.h>

@interface MapView : MKMapView {

}

@end

//MapView.m
#import "MapView.h"


@implementation MapView


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}


- (void)drawRect:(CGRect)rect {
// Drawing code
}


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


@end

//MapViewDelegate.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "AnnotationToMap.h"

@interface MapViewDelegate : NSObject <MKMapViewDelegate>{
AnnotationToMap *annotationtomap;
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation;
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;
@end

//MapViewDelegate.m
#import "MapViewDelegate.h"

@implementation MapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation{

MKPinAnnotationView *pinannotationview =
[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin"];
pinannotationview.pinColor = MKPinAnnotationColorPurple;
pinannotationview.animatesDrop = NO;
pinannotationview.multipleTouchEnabled = NO;
pinannotationview.canShowCallout = YES;
pinannotationview.enabled = YES;

annotationtomap = annotation;
return pinannotationview;
}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
[mapView selectAnnotation:annotationtomap animated:YES];
}

@end

//AnnotationToMap.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AnnotationToMap : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) coord;
-(NSString *)title;
-(NSString *)subtitle;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

//AnnotationToMap.m
#import "AnnotationToMap.h"


@implementation AnnotationToMap
@synthesize coordinate;

-(id)initWithCoordinate:(CLLocationCoordinate2D) coord{
self = [super init];
coordinate = coord;
return self;
}

-(NSString *)title{
return @"タイトルでぇーす";
}

-(NSString *)subtitle{
return @"サブタイトルでーす";
}

@end

表示結果はこのようになります。
Photobucket

0 コメント:

コメントを投稿