又是一周过去了,在这周当中,我们又学了一个新的视图,mapView,地图。
MKMapView : 地图视图
A 需要导入两个框架 : #import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
B 需要在 info.plist 文件中加入 下面的条件 :
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
这两个条件是,授权打开你的位置的两种类型,第一个是 始终,第二个是,需要时才开启。
C 需要把 项目 的最上面的 Capabilities 中的 Maps 开启。
D 在 storyboard 的中画出一个 MapView(其类型是 MKMapView),并连接到程序文件中 :
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
E 在 storyboard 中,选中 MapView ,在其右侧的工具栏中,打开第四个,选中 Allows 和 Shows 中的所有
的属性。 其中的 User Location 是现实用户的当前的位置。
F 创建一个 全局的 管理者,并开启授权。
_manager = [[CLLocationManager alloc] init];
//请求授权
[_manager requestWhenInUseAuthorization];
G 使用 mapView 显示地图。
/*
MKUserTrackingModeNone = 0, // 不会跟踪用户所在的位置
MKUserTrackingModeFollow, // 会自动跟踪用户所在的位置,但不会有指向。
MKUserTrackingModeFollowWithHeading 会自动跟踪用户所在的位置,也有指向。
但是这种方式,地图不会跟着动,还需要手动。
*/
//跟踪用户位置的模式 (打开地图后,会自动定位到该用户所在的位置)
//_mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
//地图跟随移动 (该用户在地图上所处的位置,始终是地图的中心位置)
[_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];