地图篇-03.展示地图
这一小节是地图展示,在这一小节要接触到一个框架:MapKit
1.展示地图
展示地图特别简单,就是调用系统的地图,有两种方式,直接上代码
第一种方式:
导入头文件
#import <MapKit/MapKit.h>
然后输入以下代码:
1 // 1.代码展示地图
2 MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
3
4 [self.view addSubview:mapView];
运行可见:
细心的朋友能开到地图右下角有一个高德地图,这是苹果自带的框架使用的数据是高德的,但是在国外的话就不会显示这个,可以去试试改IP显示.
第二种方法:
通过storyboard来创建
自动布局前面说过了,这里就不讲了.
注意在使用storyboard拖入mapView的时候,需要导入框架和头文件
运行得到同样的效果.
2.用户位置(蓝色小圆点)
前面已经讲到了用户位置,这里不多讲,直接上代码
1 //
2 // ViewController.m
3 // 03.展示地图
4 //
5 // Created by admin on 16/5/25.
6 // Copyright © 2016年 KXZDJ. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import <MapKit/MapKit.h>
11
12 @interface ViewController ()
13 @property (weak, nonatomic) IBOutlet MKMapView *mapView;
14 //这里没有导入CLLocation头文件的原因是MapKit包含了
15 @property (nonatomic, strong) CLLocationManager *mgr;
16
17 @end
18
19 @implementation ViewController
20
21 -(CLLocationManager *)mgr {
22 if (!_mgr) {
23 _mgr = [[CLLocationManager alloc] init];
24 //这里不需要用到代理方法,所以不设置代理
25 }
26 return _mgr;
27 }
28
29 - (void)viewDidLoad {
30 [super viewDidLoad];
31 //获取用户位置
32 self.mapView.showsUserLocation = YES;
33 //请求用户授权,然后在info.plist文件里面配置字段NSLocationAlwaysUsageDescription
34 //这里我就不判断了,直接添加always字段.
35 [self.mgr requestAlwaysAuthorization];
36
37 }
38
39 - (void)didReceiveMemoryWarning {
40 [super didReceiveMemoryWarning];
41 // Dispose of any resources that can be recreated.
42 }
43
44 @end
运行效果:
现在就能看到地图上的蓝色小圆点了,但是:
点击小圆点弹出的提示框只显示了一个current location,能不能显示当前位置呢?肯定可以的啦,这里就要用到地理编码的知识了.
因为我们能获取到用户位置,就可以通过反地理编码,把他转换成当前位置.通过mapView的代理方法,显示出来.
3.用户详细地址
代码:
1 //
2 // ViewController.m
3 // 03.展示地图
4 //
5 // Created by admin on 16/5/25.
6 // Copyright © 2016年 KXZDJ. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import <MapKit/MapKit.h>
11
12 @interface ViewController ()<MKMapViewDelegate>
13 @property (weak, nonatomic) IBOutlet MKMapView *mapView;
14 //这里没有导入CLLocation头文件的原因是MapKit包含了
15 @property (nonatomic, strong) CLLocationManager *mgr;
16
17 @end
18
19 @implementation ViewController
20
21 -(CLLocationManager *)mgr {
22 if (!_mgr) {
23 _mgr = [[CLLocationManager alloc] init];
24 //这里不需要用到代理方法,所以不设置代理
25 }
26 return _mgr;
27 }
28
29 - (void)viewDidLoad {
30 [super viewDidLoad];
31 //获取用户位置
32 self.mapView.showsUserLocation = YES;
33 //请求用户授权,然后在info.plist文件里面配置字段NSLocationAlwaysUsageDescription
34 //这里我就不判断了,直接添加always字段.
35 [self.mgr requestAlwaysAuthorization];
36 //设置代理
37 self.mapView.delegate = self;
38 }
39
40 - (void)didReceiveMemoryWarning {
41 [super didReceiveMemoryWarning];
42 // Dispose of any resources that can be recreated.
43 }
44 /**
45 * 当用户位置更新的时候调用
46 *
47 * @param mapView 当前地图
48 * @param userLocation 用户位置
49 */
50 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
51 //反地理编码
52 CLGeocoder *reverseGeo = [[CLGeocoder alloc] init];
53 //MKUserLocation-userLocation的属性
54 /*
55 // 如果用户的位置正在更新,返回YES.
56 @property (readonly, nonatomic, getter=isUpdating) BOOL updating;
57
58 // 如果MKMapView.showsUserLocation=NO或者用户的位置尚未确定,返回nil.
5