一、添加大头针

地图使用的框架是MapKit

大头针走的是MKAnnotation协议

/*

注意:因为是满足协议MKAnnotation,所以没有MKAnnotation的系统大头针类,必须自定义大头针类,我自定义的为MyAnnotation
 大头针:
 在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。
 */

1.在地图上显示,所以先在延展里定义属性

///定位管理器属性
@property (nonatomic, strong) CLLocationManager *locationManager;

///显示地图
@property (nonatomic, strong) MKMapView *mapView;

 

2.构造视图

#pragma mark - 创建视图
- (void)createMapView {
    
    //创建地图,并添加到当前视图上
    self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.mapView];
    
    //设置代理
    _mapView.delegate = self;
    
    //定位
    self.locationManager = [[CLLocationManager alloc] init];
    
    //判断隐私并授权  //这一段可以参考iOS进阶_地图定位
    if (![CLLocationManager locationServicesEnabled]) {
        
        NSURL * url = [NSURL URLWithString:@"prefs:root=privacy"];
        [[UIApplication sharedApplication]openURL:url];
        
        NSLog(@"当前设备定位不可用");
    }
    
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        [self.locationManager requestWhenInUseAuthorization];
    }
    
    //设置地图的定位追踪
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //设置地图的显示类型
    _mapView.mapType = MKMapTypeStandard;
    
    //添加大头针
    [self addAnnotation];
    
}

 

3.添加大头针,给其属性赋值,并将大头针添加到地图上

#pragma mark - 添加大头针
- (void)addAnnotation {
    
    //设置位置
    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(40, 116);
    MyAnnotation *annotation1 = [[MyAnnotation alloc] init];
    annotation1.coordinate = location1;
    annotation1.title = @"北京";
    annotation1.subtitle = @"Anana's home";
    
    [_mapView addAnnotation:annotation1];
    
}

 

4.如果要自定义大头针的图片外观图片,需要在MKAnnotationView类里面调用.image属性,并将自定义的MyAnnotation属性赋给MKAnnotationView

#pragma mark - 实现自定义大头针的代理方法
//显示大头针时调用的方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    //判断是否是当前自定义的大头针类
    if ([annotation isKindOfClass:[MyAnnotation class]]) {
        //先定义一个重用标识
        static NSString *identifier = @"AnnotationOne";
        
        MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            
            //允许用户交互
            annotationView.canShowCallout = YES;
            //设置详情信息和大头针的偏移量
            annotationView.calloutOffset = CGPointMake(0, 1);
            //设置详情的左视图
            annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
            
        }
        //修改大头针视图
        annotationView.annotation = annotation;
        annotationView.image = [UIImage imageNamed:@"图片名"];
        
        return annotationView;
    } else {
        return nil;
    }
    
}

 

posted on 2016-05-31 21:34  TWFUQTN亖  阅读(1130)  评论(0编辑  收藏  举报