前回の続き:
[iPhone] Map Kitで遊ぼう:地図を出して現在位置にピンを立てる
http://blog.s21g.com /articles/ 1596
参考:
Drawing polyines or routes on a MKMapView (as an MKAnnotati
onView) – Part 2
http://spitzkoff.com /craig/?p=108
MKAnnotati onを実装するクラスを作成
MyAnnotati
1 #import <Foundation/Foundatio n.h> 2 #import <MapKit/Map Kit.h> 3 4 @interface MyAnnotati on : NSObject <MKAnnotati on> { 5 CLLocation Coordinate 2D _coordinat e; 6 } 7 8 @property (nonatomic, readonly) CLLocation Coordinate 2D coordinate ; 9 10 -(id)initWithCo ordinate:(CLLocation Coordinate 2D) coordinate ; 11 12 @end
MyAnnotati
1 #import "MyAnnotation.h" 2 3 @implementa tion MyAnnotati on 4 5 @synthesize coordinate =_coordinat e; 6 7 -(id)initWithCo ordinate:(CLLocation Coordinate 2D) c{ 8 _coordinat e=c; 9 return self; 10 } 11 12 @end
ViewContro llerでMKMapViewD elegateを実装
ポイントとしては
- .hでMKMapViewD
elegateを実装宣言 - _mapView.d
elegate = self; - -(MKAnnotati
onView )mapView:(MKMapView )mapView viewForAnn otation:(id )annotation を実装 - MyAnnotati
onなら画像を設定する:
if([annotationisKindOfCl ass:[MyAnnotati on class]]) { annotation View.image = [UIImage imageNamed :@"some.png"];
本当はMKAnnotati
MapTutoria
1 #import <UIKit/UIKit.h> 2 #import <MapKit/Map Kit.h> 3 4 @interface MapTutoria lViewContr oller : UIViewCont roller <MKMapViewD elegate> { 5 MKMapView *_mapView; 6 } 7 @end
MapTutoria
1 #pragma mark MKMapViewDelegate 2 - (MKAnnotati onView *)mapView:(MKMapView *)mapView viewForAnn otation:(id <MKAnnotati on>)annotation { 3 MKAnnotati onView *annotation View; 4 NSString* identifier = @"Pin"; 5 annotation View = (MKPinAnnot ationView*)[_mapView dequeueReu sableAnnot ationViewW ithIdentif ier:identifier ]; 6 if(nil == annotation View) { 7 annotation View = [[[MKPinAnnot ationView alloc] initWithAn notation:annotation reuseIdent ifier:identifier ] autoreleas e]; 8 } 9 if([annotation isKindOfCl ass:[MyAnnotati on class]]) { 10 annotation View.image = [UIImage imageNamed :@"some.png"]; //地図に表示したい画像を設定 11 } 12 return annotation View; 13 } 14 15 - (void)addSomeAnn otations { 16 //a location 17 double latitude = 37.3316889 99999997; 18 double longitude = -122.030731 ; 19 CLLocation Coordinate 2D _location; 20 _location. latitude = latitude; 21 _location. longitude = longitude; 22 23 CLLocation Coordinate 2D location1; 24 MyAnnotati on *annotation ; 25 26 location1. latitude = _location. latitude+0.1; 27 location1. longitude = _location. longitude+0.1; 28 annotation =[[MyAnnotati on alloc] initWithCo ordinate:location1]; 29 [_mapView addAnnotat ion:annotation ]; 30 31 location1. latitude = _location. latitude+0.5; 32 location1. longitude = _location. longitude+0.5; 33 annotation =[[MyAnnotati on alloc] initWithCo ordinate:location1]; 34 [_mapView addAnnotat ion:annotation ]; 35 } 36 37 - (void)viewDidLoa d { 38 [super viewDidLoa d]; 39 40 _mapView = [[MKMapView alloc] initWithFr ame:self.view.bounds]; 41 _mapView.showsUserL ocation=TRUE; 42 _mapView.delegate = self; 43 [self.view addSubview :_mapView]; 44 45 [self addSomeAnn otations]; 46 47 } 48
posted by
satoko
on Wed 9 Sep 2009
at 02:12