[转载]iphone开发-地图注解(地图上的大头针

[转载]iphone开发-地图注解(地图上的大头针)

  (2012-05-10 14:42:13)
标签: 

转载

分类: ios开发
iphone开发小笔记:iphone开发-地图注解(地图上的大头针)


Cocoa Touch 没有提供地图注解类,只定义了一个 MKAnnotation 协议。要创建地图注解,必须设计符合 MKAnnotation 协议的类,该类需要一个 CLLocationCoordinate2D coordinate 属性,以及 title 和 subtitle 实例方法,


一. 设计注解类:

例,注解(大头针)类:

.h

#import <MapKit/MapKit.h>


@interface LocationObject : NSObject <MKAnnotation> {

CLLocationCoordinate2D coordinate;

NSString *_titleString; //title

NSString *_subTitleString;

float _latitude; // 经度值

float _longitude; //纬度值

}


@property (nonatomicreadonly) CLLocationCoordinate2D coordinate;

@property float _latitude; // 经度值

@property float _longitude; //纬度值

@property (nonatomiccopy) NSString *_titleString; //title

@property (nonatomiccopy) NSString *_subTitleString;


- (id) initWithTitle:(NSString *)atitle latitue:(float)alatitude longitude:(float)alongitude;


@end

---------------------

.m

@implementation LocationObject

@synthesize coordinate,_latitude,_longitude,_titleString,_subTitleString;


- (id) initWithTitle:(NSString *)atitle latitue:(float)alatitude longitude:(float)alongitude

{

if(self=[super init])

{

self._titleString = atitle;

self._latitude = alatitude;

self._longitude = alongitude;

}

return self;

}


- (CLLocationCoordinate2D)coordinate;

{

    CLLocationCoordinate2D currentCoordinate;

currentCoordinate.latitude = self._latitude ;

    currentCoordinate.longitude = self._longitude;

    return currentCoordinate; 

}


// required if you set the MKPinAnnotationView's "canShowCallout" property to YES

- (NSString *)title

{

return self._titleString;

}

// optional

- (NSString *)subtitle

{

    return _subTitleString;

}


- (void)dealloc

{

[_titleString release];

[_subTitleString release];

    [super dealloc];

}


@end


二、创建、添加和删除注解

1、创建注解:

LocationObject *aLocationObject = [[LocationObject alloc]initWithTitle:nameString latitue:[latitudeString floatValuelongitude:[longitudeString floatValue]];

aLocationObject._subTitleString = addressString;


2、添加注解:

先构建一个注解数组 NSMutableArray *_mapAnnotations;

然后 

[self._mapAnnotations addObject:aLocationObject];

[self._mapView addAnnotations:self._mapAnnotations];


3、删除注解:

删除注解可执行 removeAnnotation:一次只删除一个注解,或者执行 removeAnnotation:删除一个数组中的所有项。

如果需要使地图视图回到无注解状态,可执行:

[self._mapView removeAnnotations:self._mapView.annotations];

删除其中全部注解,MKMapView  annotations 属性获取了所有注解的数组,然后从地图上全部删除。


三、注解视图 

注解对象并非视图,是描述注解的抽象类。注解视图是属于 MKAnnotationView 的子类 MKPinAnnotationView,当地图通过 addAnnotation:或 addAnnotations:添加了注解后,MKMapViewDelegate 协议的委托方法 - (void)mapView:(MKMapView *)mapViewdidAddAnnotationViews:(NSArray *)views 就会通知委托,可以在此回调方法里设置注解视图,如设置大头针颜色、添加附属按钮等,例:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

{

// Initialize each view

for (MKPinAnnotationView *mkaview in views)

{

// 当前位置 的大头针设为紫色,并且没有右边的附属按钮

if ([mkaview.annotation.title isEqualToString:@"当前位置"]) 

{

mkaview.pinColor = MKPinAnnotationColorPurple;

mkaview.rightCalloutAccessoryVie= nil;

continue;

}

 

// 其他位置的大头针设为红色,右边添加附属按钮

mkaview.pinColor = MKPinAnnotationColorRed;

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

mkaview.rightCalloutAccessoryVie= button;

}

}


四、注解视图 MKPinAnnotationView 的几个属性说明


newAnnotation.animatesDrop = YES // 大头针掉落的动画开启,NO-关闭


newAnnotation.canShowCallout=YES // 控制轻击按钮是否生成一个注解视图,默认为Yes-开启


newAnnotation.pinColor    // 设置大头针颜色,一共有三种颜色:红色(MKPinAnnotationColorRed),绿色(MKPinAnnotationColorGreen),紫色(MKPinAnnotationColorPurple



五、自动显示注解视图(Callout)

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

。。。。。。

自动显示 Callout

_myAnnotation = annotation;

[self performSelector:@selector(showCallout) withObject:selfafterDelay:0.1];

 

  return newAnnotation;

}


- (void)showCallout {

    [self._mapView selectAnnotation:_myAnnotation animated:YES]; 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值