標籤:ios mkannotation協議 為地圖添加註解
添加地圖註解,這個需要用到MKAnnotation這個協議,主要有兩個UILabel類型的屬性,title和subtitle,當使用者點擊小別針時候就會把相關資訊顯示出來,如:Google地圖實現之三添加註解 - tergol - tergol的部落格 大概的操作是這樣的,先定義一個繼承了MKAnnotation的類,第當需要加上註解的時候,就根據當前的region等資訊,執行個體化出一個對像,然後把它addAnnotation到googleMap上去就可了。為了實現MKAnnotation我們重新定義一個類來操作。建立objectiv-c的NSObject類.h標頭檔#import <Foundation/Foundation.h>#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface MapAnnotations : NSObject <MKAnnotation>{CLLocationCoordinate2D coordinate;//這個表示一點,在map中就是中心點。 NSString *subtitle; NSString *title; }-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;@property (nonatomic, retain) NSString *subtitle; @property (nonatomic, retain) NSString *title; @end.m源檔案#import "MapAnnotations.h"@implementation MapAnnotations@synthesize coordinate;@synthesize title;@synthesize subtitle;-(id)initWithCoordinate:(CLLocationCoordinate2D) c{coordinate=c;NSLog(@"%f,%f",c.latitude,c.longitude);return self;}- (void) dealloc {[title release];[subtitle release];[super dealloc];}@end好了,有了這個類,我們就可以在資料更新的地方,執行個體化它的對像,然後加在MKMapview的執行個體上,就可以了,如下:mapAnnotations=[[MapAnnotations alloc] initWithCoordinate:loc];[email protected]"TEST";[email protected]"have a try";[map addAnnotation:mapAnnotations];[mapAnnotations release];
iOS MKAnnotation協議為地圖添加註解