iOS地图之MapKit框架
MapKit框架在地图中用于显示,工程实例:
1 #import "ViewController.h"
2
3 #import <MapKit/MapKit.h>
4 #import <CoreLocation/CoreLocation.h>
5 #import "myAnotation.h"
6
7 @interface ViewController ()<MKMapViewDelegate>
8 ///定位管理器
9 @property (nonatomic, strong) CLLocationManager *manager;
10 ///显示地图的视图
11 @property (nonatomic, strong) MKMapView *mapView;
12
13 @end
14
15 @implementation ViewController
16
17 - (void)viewDidLoad {
18 [super viewDidLoad];
19
20 [self createMapView];
21
22 }
23
24 #pragma mark - 创建地图视图
25 - (void)createMapView {
26 //创建地图视图,添加到当前视图
27 self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
28 [self.view addSubview:self.mapView];
29 //设置代理
30 self.mapView.delegate = self;
31 //定位
32 self.manager = [[CLLocationManager alloc] init];
33 //判断隐私并授权
34 if (![CLLocationManager locationServicesEnabled]) {
35 NSLog(@"当前设备的定位不可用");
36 }
37 if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
38 //注意设置info.plist(参照上一篇博客CoreLocation框架)
39 [self.manager requestWhenInUseAuthorization];
40 }
41 //设置地图的定位追踪,枚举值,根据实际需求选取枚举值
42 self.mapView.userTrackingMode = MKUserTrackingModeFollow;
43 //设置地图的类型
44 self.mapView.mapType = MKMapTypeStandard;
45
46 //添加大头针
47 [self addAnotation];
48
49 }
50 #pragma mark - 添加大头针方法的响应
51 - (void)addAnotation {
52 //设置经纬度
53 CLLocationCoordinate2D location = CLLocationCoordinate2DMake(40, 116);
54 myAnotation *anotation = [[myAnotation alloc] init];
55 anotation.coordinate = location;
56 anotation.title = @"北京";
57 anotation.subtitle = @"朝阳区";
58 anotation.image = [UIImage imageNamed:@"11.png"];
59
60 [self.mapView addAnnotation:anotation];
61
62 }
63
64 #pragma mark - 代理方法
65 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
66 NSLog(@"====%@", userLocation);
67 }
68 #pragma mark - 实现自定义大头针的代理方法(显示大头针的时候才会调用的方法)
69 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
70 //判断是否是自定义的大头针
71 if ([annotation isKindOfClass:[myAnotation class]]) {
72 static NSString *identifier = @"Annotation";
73 MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
74 if (!annotationView) {
75 annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
76
77 //允许用户交互
78 annotationView.canShowCallout = YES;
79 //设置偏移量
80 annotationView.calloutOffset = CGPointMake(0, 2);
81 //设置左视图
82 annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"11.png"]];
83
84 }
85
86 //修改大头针视图
87 annotationView.annotation = annotation;
88 annotationView.image = ((myAnotation *)annotation).image;
89
90 return annotationView;
91
92 }else {
93 return nil;
94 }
95 }
96 @end
大头针模型:
1 #import <Foundation/Foundation.h>
2 #import <MapKit/MapKit.h>
3 @interface myAnotation : NSObject<MKAnnotation>
4 //大头针模型;重写协议中的三个属性coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性
5 ///坐标
6 @property (nonatomic) CLLocationCoordinate2D coordinate;
7 ///标题
8 @property (nonatomic, copy) NSString *title;
9 ///子标题
10 @property (nonatomic, copy) NSString *subtitle;
11 ///自定义大头针(换成图片)
12 @property (nonatomic, strong) UIImage *image;
13
14 @end