題外話:先學習CoreLocation的原因是:MapKit有一部分知識是基于CoreLocation,所以前面必須先了解CoreLocation
注意事項:
- 導入頭文件<MapKit/MapKit.h>
- Mapkit框架里面的所有的數(shù)據(jù)類型的前綴都是以MK開頭的
- MapKit有一個比較重要的UI控件:MKMapView ,專門用于地圖顯示
地圖顯示的概括:(導入框架<MapKit/MapKit.h>)
1.把地圖放在view上
-
2.設置地圖顯示類型
/* MKMapTypeStandard 標準類型 MKMapTypeSatellite, 衛(wèi)星圖 MKMapTypeHybrid, 混合的圖(標準+衛(wèi)星) MKMapTypeSatelliteFlyover MKMapTypeHybridFlyover */

跟蹤類型
-
3.請求獲取用戶獲取地理位置權限(需要在info.plist里面進行配置)
[self.locationManger requestAlwaysAuthorization]; 配置代碼:NSLocationAlwaysUsageDescription -
4.追蹤用戶的位置(這樣才可以顯示大頭針,也就是地圖上藍色的小點點)
/* MKUserTrackingModeNone = 0, 不追蹤,不準確 MKUserTrackingModeFollow, 追蹤 MKUserTrackingModeFollowWithHeading, 追蹤并且獲取用戶方向 */ -
5.給MKMapView的對象掛代理,當在4中追蹤到用戶的位置之后就會調用下面的方法
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { }

mapView的代理方法
- 6.點擊大頭針顯示用戶的位置信息(這步操作是在第5步里面的方法中完成的)
提示:只要用戶的地理位置發(fā)生改變下面的方法才會被調用,位置不發(fā)生改變就不會被調用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
6.1 利用反地理編碼,利用用戶的經緯度來獲取用戶的位置名字,首先要在外面建立一個CLGeocoder *geocoder對象
6.2 調用一個方法(如下圖):userLocation是大頭針模型
第一個參數(shù)直接是用戶的位置:userLocation.location
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placekmark = [placemarks firstObject];
userLocation.title = placekmark.name;
userLocation.subtitle = placekmark.locality;
}];
6.3 效果如下圖
}

6.2調用的方法

6.3反地理編碼之后的值

關于打大頭針的知識
具體的代碼如下:
提示:(運行下面的代碼之前記得在info.plist里面進行權限配置)
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property(nonatomic,strong) MKMapView *mapView;
@property(nonatomic,strong) CLLocationManager *locationManger;
@property(nonatomic,strong) CLGeocoder *geocoder;
@end
@implementation ViewController
//1.顯示地圖
-(MKMapView *)mapView
{
if (!_mapView) {
_mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:_mapView];
}
return _mapView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//2.設置地圖顯示類型
self.mapView.mapType = MKMapTypeStandard;
/*
MKMapTypeStandard 標準類型
MKMapTypeSatellite, 衛(wèi)星圖
MKMapTypeHybrid, 混合的圖(標準+衛(wèi)星)
MKMapTypeSatelliteFlyover
MKMapTypeHybridFlyover
*/
//3用戶定位權限跟蹤設置
[self.locationManger requestAlwaysAuthorization];
//4.如果想利用MapView獲取用戶的位置,可以追蹤用戶的誒之
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
/*
MKUserTrackingModeNone = 0, 不追蹤,不準確
MKUserTrackingModeFollow, 追蹤
MKUserTrackingModeFollowWithHeading, 追蹤并且獲取用戶方向
*/
//注意在IOS8里面想要追蹤用戶的位置,必須自己請求隱私權限
//5.關于地圖的一些其他設置
//mapView.rotateEnabled = YES;
/*
scrollEnabled 支持拖動
rotateEnabled 支持旋轉
pitchEnabled 支持縮放
*/
//mapView.showsUserLocation = YES;
//6.鎖定位置直接移動到指定位置
self.mapView.delegate = self;
}
#pragma mark MKMapViewDelegate
/*
* 每次更新到用戶的位置就會調用(調用不 頻繁,只有位置改變才會調用)
*
* @param mapView 促發(fā)事件的控件
*
* @param userLocation 大頭針模型
*
*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/*
地圖上藍色的點就稱之為大頭針
大頭針可以擁有標題/子標題/位置信息
MKUserLocation 大頭針是由大頭針的模型來確定
*/
//6.1設置大頭針顯示的內容
// userLocation.title = @"藍科";
// userLocation.subtitle = @"牛逼";
//6.2移動地圖到用戶所在的位置
//獲取用戶當前所在位置的經緯度,并且設置為地圖的中心點
//[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
//6.3設置地圖顯示的區(qū)域
//獲取用戶的位置
//
// CLLocationCoordinate2D center = userLocation.location.coordinate;
//
// //將用戶當前的位置作為顯示區(qū)域的中心點,并且指定需要顯示的跨度范圍
// //
// MKCoordinateSpan span = MKCoordinateSpanMake(100, 100);
// MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
//
// //設置顯示區(qū)域
// [self.mapView setRegion:region animated:YES];
//
//利用飯地理編碼獲取位置之后設置標題
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placekmark = [placemarks firstObject];
userLocation.title = placekmark.name;
userLocation.subtitle = placekmark.locality;
NSLog(@"==%@ ==%@",userLocation.title, userLocation.subtitle);
}];
}
-(CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
-(CLLocationManager *)locationManger
{
if (!_locationManger) {
_locationManger = [[CLLocationManager alloc]init];
}
return _locationManger;
}
@end

總體結構圖