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;
}