apView.showsUserLocation=NO或者用户的位置尚未确定,返回nil.
70 @property (readonly, nonatomic, nullable) CLLocation *location;
71
72 // 如果不适用MKUserTrackingModeFollowWithHeading返回nil;
73 @property (readonly, nonatomic, nullable) CLHeading *heading NS_AVAILABLE(10_9, 5_0);
74
75 // 描述用户当前位置的文本.
76 @property (nonatomic, copy, nullable) NSString *title;
77
78 // 描述用户当前位置的详细信息.
79 @property (nonatomic, copy, nullable) NSString *subtitle;
80 */
81 [reverseGeo reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
82 //判断,如果返回的地标为空,或者error存在的时候
83 if (placemarks.count == 0 || error) {
84 NSLog(@"地理编码失败");
85 return;
86 }
87
88
89
90 //根据userLocation.location进行反地理编码获得的地址只有一个(地理编码小节中有讲)
91 CLPlacemark *clp = [placemarks firstObject];
92 //设置用户当前位置的地址
93 userLocation.title = clp.name;
94 //设置用户当前位置的详细信息
95 userLocation.subtitle = @"那小子真帅...";//这里可以打印街道,门牌号等等,这里举例.
96
97
98 }];
99
100 }
101
102 @end
运行效果如下:
在上面代码中第42行,我们不知道这个范围应该是多少,不用急,有两个代理方法能带我们装X带我们飞.
代码:
1 /**
2 * 地图的范围已经改变的时候调用
3 */
4 -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
5 //纬度的跨度
6 CLLocationDegrees latitudeDelta = mapView.region.span.latitudeDelta;
7 //经度的跨度
8 CLLocationDegrees longitudeDelta = mapView.region.span.longitudeDelta;
9
10 NSLog(@"纬度的跨度:%f,经度的跨度:%f",latitudeDelta,longitudeDelta);
11 }
12 /**
13 * 地图的范围将要改变的时候调用
14 */
15 -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
16
17 }
这两个方法中能获取到当前地图的跨度,在模拟器上按住option键然后左键上下滑动,会看到打印如下:
可以自己试一下.
但是这种方法有点麻烦,要获取坐标等一系列操作,下面介绍一种方法,利用mapView的一个属性userTrackingMode
代码:
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
41
42 //跟踪用户位置(系统会自动给你设置一个比较合适的范围)
43 self.mapView.userTrackingMode = MKUserTrackingModeFollow;
44
45 //中心点
46 // CLLocationCoordinate2D center = CLLocationCoordinate2DMake(30.67, 104.06);
47 // //跨度
48 // MKCoordinateSpan span = MKCoordinateSpanMake(1.0, 1.0);
49 // //范围
50 // MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
51 //
52 // //地图范围
53 // [self.mapView setRegion:region];
54
55 }
56
57 - (void)didReceiveMemoryWarning {
58 [super didReceiveMemoryWarning];
59 // Dispose of any resources that can b |