2010-08-30 45 views
0

我正在使用Mapkit,並且必須在地圖中顯示註釋,但我無法顯示註釋。這是我的代碼:在地圖工具包中顯示註釋

@interface MyMapView : UIViewController <MKAnnotation,MKMapViewDelegate>{ 

MKMapView *Obj_Map_View; 
MKPlacemark *pmark; 
MKReverseGeocoder *geocoder1; 
} 

@end 


#import "MyMapView.h" 
@implementation MyMapView 

- (id)init { 
    if (self = [super init]) { 

    } 
    return self; 
} 

- (void)loadView { 
    [super loadView]; 
    Obj_Map_View = [[MKMapView alloc]initWithFrame:self.view.bounds]; 
    Obj_Map_View.showsUserLocation =YES; 
    Obj_Map_View.mapType=MKMapTypeStandard; 
    [self.view addSubview:Obj_Map_View]; 
    Obj_Map_View.delegate = self; 

    CLLocationCoordinate2D cord = {latitude: 19.120000, longitude: 73.020000}; 
    MKCoordinateSpan span = {latitudeDelta:0.3, longitudeDelta:0.3}; 
    MKCoordinateRegion reg= {cord,span}; 
    [Obj_Map_View setRegion:reg animated:YES]; 
    //[Obj_Map_View release]; 
} 

- (NSString *)subtitle{ 
    return @"Sub Title"; 
} 

- (NSString *)title{ 
    return @"Title"; 
} 

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    MKPinAnnotationView *annov = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Current location"]; 
    annov.animatesDrop = TRUE; 
    [annotation title][email protected]"Current location"; 
    annov.canShowCallout = YES; 
    [annov setPinColor:MKPinAnnotationColorGreen]; 
    return annov; 
} 

上面的代碼工作正常,並顯示一個地圖,但沒有註釋。

+0

可以編輯您的問題,以便源代碼可讀性?謝謝。 – 2010-08-30 12:52:49

+0

檢查此鏈接: http://www.edumobile.org/iphone/iphone-programming-tutorials/mapkit-example-in-iphone/ – 2012-07-26 10:27:26

回答

2

通常,符合MKAnnotation協議的類不是視圖控制器,它是數據類。

您需要創建另一個類,我將爲該示例調用「MyLandmarks」。

@interface MyLandmarks : NSObject <MKAnnotation> 
    // Normally, there'd be some variables that contain the name and location. 
    // And maybe some means to populate them from a URL or a database. 
    // This example hard codes everything. 

@end 


@implementation MyLandmarks 
-(NSString*)title { 
    return @"'ere I am, J.H."; 
} 

-(NSString*)subtitle { 
    return @"The ghost in the machine."; 
} 

-(CLLocationCoordinate2D) coordinate { 
    CLLocationCoordinate2D coord = {latitude: 19.120000, longitude: 73.020000}; 

    return coord; 
} 
@end 

然後,在你MyMapView類添加某個適當的:其他的Objective-C開發與您合作會明白

MyLandmark *landmark = [[[MyLandmark alloc]init]autorelease]; 
[Obj_Map_View addAnnotation:landmark]; 

幾個其他位:

  • 爲了避免混淆,如果它從UIViewController下降,請不要致電MyMapView。改爲將其稱爲MyMapViewController
  • 類以大寫字母開頭,變量以小寫字母開頭。兩者都是CamelCased。 Obj_Map_View應該是objMapView
+0

感謝約翰它工作 – Radix 2010-09-17 13:49:15

0

要添加註釋使用:addAnnotation:

讀到它here