2012-09-13 34 views
1

下面的代碼適用於獨特的引腳和一個註釋。我想適應它而不需要太多更改以顯示更多的引腳,位置和註釋。將此代碼重複用於多個mapkit引腳註釋

有一個名爲MyAnnotationPins下列行類:

MyAnnotationPins.h

@interface MyAnnotationPins : NSObject < MKAnnotation> 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly, copy) NSString *title; 
@property (nonatomic, readonly, copy) NSString *subtitle; 
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle; 

MyAnnotationPins.m

@synthesize coordinate; 
@synthesize subtitle; 
@synthesize title; 
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle 
{ 
    self = [super init]; 
    if (self) 
    { 
     coordinate = annotCoordinate; 
     subtitle = [[NSString alloc] initWithString:annotSubtitle]; 
     title = [[NSString alloc] initWithString:annotTitle]; 
    } 

    return self; 
} 

,並在視圖控制器以下代碼:

SecondViewController.h

import "MyAnnotationPins.h" 


@interface SecondViewController : UIViewController <MKMapViewDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mapView;  
@property (strong, nonatomic) MyAnnotationPins* biblioAnnotation; 

在SecondViewController.m實現:

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    mapView.delegate = self; 

    MKCoordinateRegion mapRegion; 
    mapRegion.center.latitude=-18.924129; 
    mapRegion.center.longitude=-48.283963; 
    mapRegion.span.latitudeDelta=0.2; 
    mapRegion.span.longitudeDelta=0.2; 

    [mapView setRegion:mapRegion animated:YES]; 

    ///// This is just for One annotaion/Pin on Map ///// 
    CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963); 
    biblioAnnotation = [[MyAnnotationPins alloc] 
          initWithCoordinate:parliamentLocation 
          title:@"Ponto Biblioteca" 
          subtitle:@"Taxi proximo"]; 
    [mapView addAnnotation:biblioAnnotation]; 

如果你想要一個以上的引腳和annotaion複製下面的CLLocation實例,並更改以下atributes

CLLocationCoordinate2D secondLocation = CLLocationCoordinate2DMake(another latitude, another longitude); 
secondAnnotation = [[MyAnnotationPins alloc] 
          initWithCoordinate:secondLocation 
          title:@"Second Title" 
          subtitle:@"Second subtitle"]; 
[mapView addAnnotation:secondAnnotation]; <code> 

等等在第三,第四五等等。不要忘記在你的視圖控制器中創建secondLocation proerty,像SecondViewController.h中的第一個,還有 @synthes IZE在SecondViewController.m secondAnnotation屬性文件

@property (strong, nonatomic) MyAnnotationPins* secondAnnotation; 
+0

多個位置的數據來自哪裏?只需創建多個'MyAnnotationPins'對象並在其上調用'addAnnotation'。確切的問題是什麼? – Anna

+0

嗨,安娜,謝謝你的回覆。 –

+0

多個地點將來自我從Google地圖獲取的緯度和經度列表。所以我會在代碼中手動創建其他引腳,但我不知道如何在此代碼中創建多個MyAnnotationPins對象。我將首先在.h中創建一個新的MyAnnotationPin * newLocation,並在.m文件中添加並更新MKCoordinateRegion和CLLocationCoordinate2D代碼。什麼和在哪裏改變他們?緯度,經度,標題,字幕? –

回答

0

在循環中添加他們像這樣

for(X in Y) 
{ 
CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963); 
MyAnnotationPins* biblioAnnotation = [[MyAnnotationPins alloc] 
         initWithCoordinate:parliamentLocation 
         title:@"Ponto Biblioteca" 
         subtitle:@"Taxi proximo"]; 
[mapView addAnnotation:biblioAnnotation]; 
} 

當你遍歷你的清單,你可以得到每一個正確的座標和標題。這樣你列表可以增長或縮小,你不需要添加第三,第四或第五個註釋屬性。

+0

Yeaah克雷格,非凡......乾杯! –