1、介绍
(1)MapKit->MKMapView所有以地图相关的都是以MK开头
(2)使用地图功能也需要开启定位服务向用户请求定位授权
->CLloction 对象 去请求授权
(3)功能
<1>展示地图
<2>展示用户所在位置
<3>添加大头钉
展示大头钉上的内容
(4)iOS9.0之后的改变
(1)可以显示交通的情况
(2)可以显示地图比例
(3)可以显示罗盘
2、使用
1、MKMapViw:创建地图对象
2、MKUserLocation:用户大头针资料 大头针的数据模型 大头针上面显示的内容
是根据大头针的数据模型来决定的
3、MKAnnotation:大头针数据资料 大头针的数据模型是所有大头针模型的父类
不能直接使用 可以使用他的子类
<1>系统提供的子类
<2>自定义大头针数据模型
4、MKPointAnnotation:大头针模型
5、 MKPinAnnotationView:大头针控件
6、 MKAnnotationView:大头针控件
7、 MKCoordinateSpan:经纬度的跨度 跨度越小显示的范围越小越精准
8、 MKCoordinateRegion:经纬度的范围
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<MKMapViewDelegate>{
CLLocationManager *loctionManage;
MKCoordinateSpan mySpan;
}
@property(nonatomic,strong)CLGeocoder *geocoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
loctionManage = [[CLLocationManager alloc]init];
[loctionManage requestWhenInUseAuthorization];
// UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//
// view.backgroundColor = [UIColor redColor];
//
// [mapView addSubview:view];
//设置地图的样式
/*
MKMapTypeStandard :标准样式
MKMapTypeSatellite :卫星地图
MKMapTypeHybrid :鸟瞰 混合
MKMapTypeSatelliteFlyover :iOS9 卫星地图的立体样式
MKMapTypeHybridFlyover:鸟瞰混合 立体样式
*/
mapView.mapType =MKMapTypeStandard;
//显示用户位置
/*
使用地图 显示用户位置的时候 无法定位到用户位置
1⃣️showsUserLocation 未设置成YES
2⃣️为创建定位对象 请求用户授权
3⃣️使用定位服务目的的描述和发送的请求不匹配 或者未在info.plist中添加
4⃣️用户 未在 设置中开启定位服务
*/
//显示用户位置
mapView.showsUserLocation = YES;
//设置跟踪用户的样式
/*
MKUserTrackingModeNone 不跟踪
MKUserTrackingModeFollow 跟踪
MKUserTrackingModeFollowWithHeading 根据航向跟踪
*/
mapView.userTrackingMode = MKUserTrackingModeFollow;
/*
pitchEnabled 捏合
rotateEnabled 旋转
scrollEnabled 滚动
zoomEnabled 缩放
*/
// mapView.zoomEnabled = NO;
// mapView.scrollEnabled = NO;
// mapView.pitchEnabled= NO;
// mapView.rotateEnabled = NO;
//在标准下才会有效果
mapView.showsBuildings = YES;
// 显示建筑物
// 在MKMapTypeStandard才会有效果
// myMapView.showsBuildings = YES;
// 显示热门点
// 在 MKMapTypeStandard and MKMapTypeHybrid 有效果
mapView.showsPointsOfInterest = YES;
//显示标尺
mapView.showsScale = YES;
//显示交通状态
mapView.showsTraffic = YES;
//显示罗盘
mapView.showsCompass =YES;
mapView.delegate = self;
[self.view addSubview:mapView];
mySpan = MKCoordinateSpanMake(0.1,0.1);
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
//MKUserLocation用户大头针的数据模型->里面存储的内容就是点击大头针展现的内容
//反地理编码
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//通过地标的数组找到第一个地标元素 的到里面的name
userLocation.title = placemarks.firstObject.name;
userLocation.subtitle = placemarks.firstObject.thoroughfare;
}];
// userLocation.title =@"正客源";
// userLocation.subtitle =@"人才孵化基地";
//MKCoordinateSpan 经纬度的跨度
//设置用户所在位置一个范围
// @property (nonatomic) MKCoordinateRegion region;
// - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
//设置用户在地图上为中心点
// - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated
[mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
//这个Region是MapKit里面<#(MKCoordinateRegion)#>
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, mySpan);
[ mapView setRegion:region animated:YES];
//MKCoordinateRegion 经纬度的范围
}
-(CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
//地图范围将要发生改变的时候调用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
}
//改变完成的时候调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
//动态的设置跨度
mySpan = mapView.region.span;
}