您的位置:首页 > 移动开发 > IOS开发

iOS MKAnnotation协议为地图添加注解

2014-10-27 10:15 393 查看
添加地图注解,这个需要用到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];
mapAnnotations.title=@"TEST";
mapAnnotations.subtitle=@"have a try";
[map addAnnotation:mapAnnotations];
[mapAnnotations release];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: