地图 大头针
2016-05-31 20:16
260 查看
MyAnnotation.h
ViewController.m
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject <MKAnnotation> // 重写协议中的三个属性coordinate(标记位置)、title(标题)、subtitle(子标题) /** 坐标 */ @property (nonatomic) CLLocationCoordinate2D coordinate; /** 标题 */ @property (nonatomic, copy) NSString *title; /** 子标题 */ @property (nonatomic, copy) NSString *subtitle; // 自定义大头针 /** 自定义大头针显示的图片 */ @property (nonatomic, strong) UIImage *image; @end
ViewController.m
#import "ViewController.h" // 地图使用的是MapKit框架 #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> #import "MyAnnotation.h" @interface ViewController () <MKMapViewDelegate> /** 定位管理器 */ @property (nonatomic, strong) CLLocationManager *locationManager; /** 显示地图的视图 */ @property (nonatomic, strong) MKMapView *mapView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /* 大头针:在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。 */ // 创建视图 [self createMapView]; } #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]; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"当前设备定位不可用"); } if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) { [self.locationManager requestWhenInUseAuthorization]; } // 设置地图的定位追踪 _mapView.userTrackingMode = MKUserTrackingModeFollow; // 设置地图的类型 _mapView.mapType = MKMapTypeStandard; // 添加大头针 [self addAnnotation]; } #pragma mark - 添加大头针 - (void)addAnnotation { // 设置位置 CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(40, 116); CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(39, 121); CLLocationCoordinate2D location3 = CLLocationCoordinate2DMake(31, 121); MyAnnotation *annotation1 = [[MyAnnotation alloc] init]; annotation1.coordinate = location1; annotation1.title = @"北京"; annotation1.subtitle = @"大风起兮云飞扬"; #warning 添加代码 annotation1.image = [UIImage imageNamed:@"1.jpg"]; MyAnnotation *annotation2 = [[MyAnnotation alloc] init]; annotation2.coordinate = location2; annotation2.title = @"大连"; annotation2.subtitle = @"哈哈哈"; #warning 添加代码 annotation2.image = [UIImage imageNamed:@"1.jpg"]; MyAnnotation *annotation3 = [[MyAnnotation alloc] init]; annotation3.coordinate = location3; annotation3.title = @"上海"; annotation3.subtitle = @"嘿嘿嘿"; #warning 添加代码 annotation3.image = [UIImage imageNamed:@"1.jpg"]; [_mapView addAnnotations:@[annotation1, annotation2, annotation3]]; } #warning 增加代码 #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.jpg"]]; } // 修改大头针视图 annotationView.annotation = annotation; annotationView.image = ((MyAnnotation *)annotation).image; return annotationView; } else { return nil; } } #pragma mark - 代理方法 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"%@", userLocation); } @end
相关文章推荐
- C 字符串中sizeof() 和 strlen()
- 工资数组类
- HDU 4568 Hunter
- Guice 注入--(privateModule,intall(),expose())
- CF_602A - Two Bases(进制转换—水题)
- HTML5+CSS3-第二节(浏览器前缀、css新特征、文本溢出、新的颜色设定、透明设定、文本填充色、文本边框色、圆角)
- peda的帮助文档(自己翻译)
- 在Android应用中使用自定义证书的HTTPS连接(下)
- 练习三 Problem T
- 二分图匈牙利算法模板
- [置顶] android.support.v7.widget.SearchView开发记录(一)
- HTML5+CSS3-第一节(文档类型声明、新增标签)
- Hat's Fibonacci
- Middle-题目122:220. Contains Duplicate III
- linux搭建zookeeper集群
- Hadoop基础之HDFS
- SQL SERVER 索引碎片
- 《校园封神榜》第二阶段个人工作总结——第八天
- 列表生成式的复习以及生成器的练习, 杨辉三角实例(非常巧妙)
- Python装饰器详解