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 (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property float _latitude; // 经度值
@property float _longitude; //纬度值
@property (nonatomic, copy) NSString *_titleString; //title值
@property (nonatomic, copy) 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 floatValue] longitude:[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.rightCalloutAccessoryView = nil;
continue;
}

// 其他位置的大头针设为红色,右边添加附属按钮
mkaview.pinColor = MKPinAnnotationColorRed;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mkaview.rightCalloutAccessoryView = 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];
}



MKMapView学习笔记(转)
一.基本知识
     目前主流的智能手机大部分都支持GoogleMap地图程序,而手机上的地图程序确实能给我们的出行带来很大的方便。在iPhone中利用MapKit框架可以很方便的显示Google地图,并且可以在地图上添加标注。

二.具体介绍

1.MKMapView的显示

(1)创建MKMapView
CGRect rect = CGRectMake(0, 20, 320, 460);
MKMapView *mapView = [[MKMapView alloc] initWithFrame:rect];
(2)设定经纬度
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude=24.148926;
theCoordinate.longitude=120.715542;
(3)设定显示范围
MKCoordinateSpan theSpan;
theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
(4)设置地图显示的中心及范围
MKCoordinateRegion theRegion;
theRegion.center=theCoordinate;
theRegion.span=theSpan;
(5)设置地图显示的类型及根据范围进行显示
[mapView setMapType:MKMapTypeStandard];
[mapView setRegion:theRegion];
完成这些步骤,再把mapView添加到当前view中就可以显示了。

2.在MKMapView上添加标注
(1)和标注相关的类及协议
       (a)MKAnnotation Protocol
              标注必须实现这个协议,有三个属性,coordinate,title和subtitle,其中coordinate属性必须设置。
              @property (nonatomic, readonly) CLLocationCoordinate2D coordinate
     (b)MKAnnotationView
              设置好Annotation后就可以用这个把标注在地图上显示出来, 
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
              其比较重要的属性有
@property (nonatomic, retain) UIImage *image
自定义在地图上标注的图片

@property (nonatomic) BOOL canShowCallout
设置点击后能否弹出标注

@property (retain, nonatomic) UIView *rightCalloutAccessoryView
property (retain, nonatomic) UIView *leftCalloutAccessoryView
设置在标注的左右边点击后进一步弹出附属的View

(c)MKPinAnnotationView

这是以大头针的方式显示标注,继承自MKAnnotationView,同时添加了两个属性
@property (nonatomic) MKPinAnnotationColor pinColor
设置大头针的颜色,有红绿紫三种颜色可选择

@property (nonatomic) BOOL animatesDrop
设置大头针是否以掉下来的动画方式显示

(2)在地图上添加Annotation的步骤
    (a)创建一个实现MKAnnotation协议的类,在该类的初始化函数中给其coordinate属性设置
    (b)用上述方法创建Annotation
    (c)把创建的Annotation用addAnnotation的方法添加到MapView中
    (d)实现MKMapViewDelegate代理,在代理函数

- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id <MKAnnotation>)annotation中把Annotation以MKPinAnnotationView或MKAnnotationView的方式标注在地图上上显示。

 

 

 

原帖地址:http://www.cocoachina.com/bbs/read.php?tid=123176&fpage=15


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值