/**
* Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException',
reason: 'Could not instantiate class named MKMapView'
*
* @ 如果storyboard中用到了地图, 需要导入MapKit框架
*/
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
/**
* 地图
*/
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (CLLocationManager *)mgr{
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 在 ios8 中如果想 追踪用户隐私信息, 必须主动请求隐私权限
if (IOS8) {
[self.mgr requestAlwaysAuthorization];
}
// self.mapView.scrollEnabled = NO;
self.mapView.rotateEnabled = NO;
/** 1. 设置显示类型
*
MKMapTypeStandard --- 标准(默认)
MKMapTypeSatellite, --- 卫星
MKMapTypeHybrid --- 混合的
*/
self.mapView.mapType = MKMapTypeStandard;
// 2. 如果想利用 MapKit 获取用户的位置, 可以追踪
/**
MKUserTrackingModeNone = 0, // the user's location is not followed
MKUserTrackingModeFollow, // the map follows the user's location --- 追踪
MKUserTrackingModeFollowWithHeading, // the map follows the user's location and heading -- 追踪并获得方向
*/
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
// 3. 设置代理
self.mapView.delegate = self;
}
#pragma mark - MKMapViewDelegate
/**
* 地图区域改变完成 会调用此方法
*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
LogYellow(@"地图区域改变完成");
/**
*
CLLocationCoordinate2D center;
MKCoordinateSpan span;
*/
LogCyan(@"%f --- %f",self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}
/**
* 地图区域即将改变 会调用此方法
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
LogYellow(@"地图区域即将改变");
}
*/
/**
* 每次更新用户位置就会调用此方法(调用不频繁, 只有位置变化才会调用)
* @mapView 触发事件的空间
* @userLocation 大头针的模型
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/**
* 地图上蓝色的点, 称之为大头针
大头针: title - 标题
subtitle - 子标题
location - 位置
heading - 方向
*/
LogRed(@"%f -- %f",userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// 设置大头针显示内容
// userLocation.title = @"JGL";
// userLocation.subtitle = @"NX";
// 利用反地理编码, 设置标题
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks firstObject];
LogMagenta(@"%@",placeMark);
userLocation.title = placeMark.name;
userLocation.subtitle = placeMark.locality;
}];
// 移动地图到当前用户所在位置
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
// 设置地图显示的区域
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 指定经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.0001);
// 将用户的当前位置 设置为中心点, 并且制定显示的跨度
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
}