2011-06-27 204 views
15

是否有一個簡單的示例項目MKAnnotation?我不知道在「addAnnotation」上傳遞什麼,因爲它需要一些「id<Annotation>」。 開發人員網站上的例子都是使用數組或解析器等,我只需要一個非常簡單的例子來首先了解整個事情的工作原理。MKAnnotation,簡單示例

在此先感謝..

編輯:

我成立專班Pin。在.h它寫道

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

@interface Pin : NSObject <MKAnnotation> { 
CLLocationCoordinate2D coordinate; 
NSString *title; 
NSString *subTitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subTitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 
@end 

根據onnoweb回答。

在Pin.m,我實現這樣的,根據不同的例子:

#import "Pin.h" 

@implementation Pin 

@synthesize title, subTitle; 

- (CLLocationCoordinate2D) coordinate { 
CLLocationCoordinate2D coords; 
coords.latitude = coordinate.latitude; //i want it that way 
coords.longitude = 7.1352260; //this way it works 

return coords; 

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

- (NSString *) subtitle { 
return @"Thats the sub"; 
} 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description { 

return self; 
} 

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

@end 

所以,如果我用手工設置的座標,如指出的評論,它的工作原理。但我希望能夠像第一條評論中那樣動態設置值。我嘗試了各種方法,但都沒有解決。因爲你沒有指定- (CLLocationCoordinate2D) coordinate我試圖合成coordinate,但是這樣也不起作用。

您能告訴我您的MapPin.m

編輯2:

我設置mapCenter

- (void)viewWillAppear:(BOOL)animated { 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.delegate = self; 
[self.locationManager startUpdatingLocation]; 

CLLocation *curPos = locationManager.location; 
location = curPos.coordinate; 

CLLocationCoordinate2D mapCenter; 
mapCenter.latitude = location.latitude; 
mapCenter.longitude = location.longitude; 

MKCoordinateSpan mapSpan; 
mapSpan.latitudeDelta = 0.005; 
mapSpan.longitudeDelta = 0.005; 

MKCoordinateRegion mapRegion; 
mapRegion.center = mapCenter; 
mapRegion.span = mapSpan; 

mapKitView.region = mapRegion; 
mapKitView.mapType = MKMapTypeStandard; 
mapKitView.showsUserLocation=TRUE; 
} 

什麼不對?

回答

34

您需要有一個類實現MKAnnotation協議。下面是我的一個項目一個片段:

@interface MapPin : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly) NSString *title; 
@property (nonatomic, readonly) NSString *subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description; 

@end 

然後,在你創建的地圖,你會叫:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""]; 
[map addAnnotation:pin]; 

編輯:

這裏是實現:

@implementation MapPin 

@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description { 
    self = [super init]; 
    if (self != nil) { 
     coordinate = location; 
     title = placeName; 
     [title retain]; 
     subtitle = description; 
     [subtitle retain]; 
    } 
    return self; 
} 

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


@end 

希望這會有所幫助, Onno。

+0

謝謝,這已經幫了很多。就像我新的谷歌什麼谷歌,並得到它運行,但我不明白。請查看我的更新問題,瞭解我目前使用的代碼。我不知道.m應該是什麼樣.. – Nareille

+0

請參閱我編輯的答案。 – onnoweb

+0

謝謝,這有助於再次:)一些奇怪行爲的最後一個問題:我沒有在我的.h中定義類似「座標」的東西,但Xcode告訴我需要綜合它。如果我這樣做,我的地圖中心向西移動了幾百米。如果我忽略了綜合,它又重新合理。如何擺脫警告? – Nareille