だいぶ時間があいてしまいましたが、今日は、地図上にAnnotationを表示する方法について書いていきたいと思います。
プログラムが長いですが、御付き合いよろしくです。
//MapTestAppDelegate.h
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import "MapView.h" #import "AnnotationToMap.h" #import "MapViewDelegate.h" @interface MapTestAppDelegate : NSObject 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]; [window addSubview:mapview]; [mapview release]; [window makeKeyAndVisible]; } - (void)dealloc { [window release]; [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{ MKAnnotationView *annotationview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]; annotationview.multipleTouchEnabled = NO; annotationview.canShowCallout = YES; annotationview.enabled = YES; annotationtomap = annotation; return annotationview; } - (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 |
実行結果はこのようになります。
参考:
Trigger MKAnnotationView Callout bubble
Using iPhone SDK MapKit Framework – A tutorial
0 コメント:
コメントを投稿