-
MapKit是用來顯示地圖的. -
MapKit的使用步驟- 導(dǎo)入框架
MapKit - 導(dǎo)入頭文件
#import <MapKit/MapKit.h> - 在
storyboard中拖拽MKMapView
- 導(dǎo)入框架
- 地圖屬性的設(shè)置
// 1. MKMapView的顯示項(xiàng) // 1.1 設(shè)置地圖的顯示樣式 /* MKMapTypeStandard 標(biāo)準(zhǔn) MKMapTypeSatellite 衛(wèi)星 MKMapTypeHybrid 混合 MKMapTypeSatelliteFlyover 3D衛(wèi)星(10_11, 9_0), MKMapTypeHybridFlyover 3D混合(10_11, 9_0), */ self.mapView.mapType = MKMapTypeStandard; // 1.2 顯示地圖上的指南針 self.mapView.showsCompass = YES; // 1.3 顯示地圖上的建筑物 self.mapView.showsBuildings = YES; // 1.4 顯示地圖上的POI點(diǎn) self.mapView.showsPointsOfInterest = YES; // 1.5 顯示地圖上的縮放比例 self.mapView.showsScale = YES; // 1.6 顯示地圖上的交通 self.mapView.showsTraffic = YES; // 1.7 顯示用戶當(dāng)前位置 // 1.7.1 在iOS8.0之后要請求用戶授權(quán) [self locationManager]; // 1.7.2 顯示用戶當(dāng)前位置(如果不設(shè)置追蹤模式,地圖不會自動放大) self.mapView.showsUserLocation = YES; // 1.7.3 設(shè)置用戶的追蹤模式 self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading; // 2. MKMapView的控制項(xiàng) // 2.1 地圖滾動 self.mapView.scrollEnabled = YES; // 2.2 地圖縮放 self.mapView.zoomEnabled = YES; // 2.3 地圖旋轉(zhuǎn) self.mapView.rotateEnabled = YES; - 地圖的使用-結(jié)合代理方法
didUpdateUserLocation和regionDidChangeAnimated// 1. 查看用戶當(dāng)前位置信息 NSLog(@"didUpdateUserLocation-------%f, %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude); // 1.1 設(shè)置標(biāo)題 userLocation.title = @"qwer"; // 1.2 設(shè)置子標(biāo)題 userLocation.subtitle = @"asdf"; // 2. 調(diào)整地圖顯示中心 // 2.1 獲取用戶的位置信息 CLLocationCoordinate2D center = userLocation.location.coordinate; // 2.2 設(shè)置地圖的中心為用戶的位置 [self.mapView setCenterCoordinate:center animated:YES]; // 3. 調(diào)整地圖顯示區(qū)域(根據(jù)代理方法獲取到的數(shù)據(jù)創(chuàng)建跨度) // 3.1 創(chuàng)建跨度 MKCoordinateSpan span = MKCoordinateSpanMake(0.001371, 0.001006); // 3.2 創(chuàng)建區(qū)域 MKCoordinateRegion region = MKCoordinateRegionMake(center, span); // 3.3 設(shè)置地圖顯示區(qū)域?yàn)閯?chuàng)建的區(qū)域 [self.mapView setRegion:region animated:YES]; - 大頭針的基本使用
- 實(shí)現(xiàn)步驟
- 創(chuàng)建地圖
- 定義大頭針數(shù)據(jù)模型類
- 創(chuàng)建大頭針數(shù)據(jù)模型
- 添加大頭針數(shù)據(jù)模型
- 代碼實(shí)現(xiàn)
/* 大頭針數(shù)據(jù)模型類 */ @interface ZQAnnotation : NSObject <MKAnnotation> /** 經(jīng)緯度 */ @property (nonatomic, assign) CLLocationCoordinate2D coordinate; /** 彈框標(biāo)題 */ @property (nonatomic, copy, nullable) NSString *title; /** 彈框子標(biāo)題 */ @property (nonatomic, copy, nullable) NSString *subtitle; @end /* 控制器view */ @interface ViewController () @property (weak, nonatomic) IBOutlet MKMapView *mapView; @end /*touchesBegan方法*/ // 1. 創(chuàng)建大頭針數(shù)據(jù)模型 ZQAnnotation *annotation = [[ZQAnnotation alloc] init]; // 1.1 設(shè)置大頭針數(shù)據(jù)模型的數(shù)據(jù) annotation.coordinate = self.mapView.centerCoordinate; annotation.title = @"qwer"; annotation.subtitle = @"asdf"; // 2. 添加大頭針數(shù)據(jù)模型 [self.mapView addAnnotation:annotation]; /* touchesMoved方法 */ NSArray *annotations = self.mapView.annotations; [self.mapView removeAnnotations:annotations];
- 實(shí)現(xiàn)步驟
- 大頭針使用場景案例
- 實(shí)現(xiàn)步驟
- 創(chuàng)建地圖
- 自定義大頭針數(shù)據(jù)模型
- 獲取手指所在的點(diǎn)
- 添加大頭針數(shù)據(jù)模型
- 標(biāo)注彈框
- 關(guān)鍵代碼
/* 場景描述:鼠標(biāo)點(diǎn)擊在地圖哪個位置, 就在對應(yīng)的位置添加一個大頭針, 并在標(biāo)注彈框中顯示對應(yīng)的城市和街道 */
pragma mark ------------------
- 實(shí)現(xiàn)步驟
pragma mark - Events
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1. 獲取手指所在的點(diǎn)
CGPoint touch = [[touches anyObject] locationInView:self.mapView];
// 2. 把點(diǎn)轉(zhuǎn)化為經(jīng)緯度
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:touch toCoordinateFromView:self.mapView];
// 3. 添加大頭針數(shù)據(jù)模型
ZQAnnotationItem *annotationItem = [self addAnnotationItemWithCoordinate:coordinate title:@"小碼哥" subTitle:@"iOS"];
// 4. 反地理編碼
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error == nil) {
// 從地標(biāo)數(shù)組中獲取地標(biāo)對象
CLPlacemark *placemark = [placemarks firstObject];
// 設(shè)置彈框標(biāo)題
annotationItem.title = placemark.name;
// 設(shè)置彈框子標(biāo)題
annotationItem.subtitle = placemark.locality;
}
}];
}
#pragma mark ------------------
#pragma mark - Methods
-(ZQAnnotationItem *)addAnnotationItemWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subTitle:(NSString *)subTitle{
// 創(chuàng)建大頭針數(shù)據(jù)模型對象
ZQAnnotationItem *annotationItem = [[ZQAnnotationItem alloc] init];
// 設(shè)置數(shù)據(jù)
annotationItem.coordinate = coordinate;
annotationItem.title = title;
annotationItem.subtitle = subTitle;
// 添加大頭針數(shù)據(jù)模型
[self.mapView addAnnotation:annotationItem];
return annotationItem;
}
```
* 知識點(diǎn)
* 獲取手指所在的點(diǎn)
* 把點(diǎn)轉(zhuǎn)化為經(jīng)緯度
* 反地理編碼
- 自定義大頭針
- 注意事項(xiàng):如果
viewForAnnotation這個方法沒有實(shí)現(xiàn),或者, 返回nil, 那么系統(tǒng)就會調(diào)用默認(rèn)的大頭針視圖 - 理論基礎(chǔ):如果想要自定義大頭針,要么是用父類MKAnnotationView , 或者是自定義的子類
- 自定義子類
MKPinAnnotationView - 用父類
MKAnnotationView
static NSString *ID = @"XMG";
// 訪問緩存池
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
// 自己創(chuàng)建
if (pinView == nil) {
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
}
// 防止數(shù)據(jù)錯亂
pinView.annotation = annotation;
// 設(shè)置彈框
pinView.canShowCallout = YES;
// 設(shè)置大頭針圖片
pinView.image = [UIImage imageNamed:@"category_3"];
// 設(shè)置彈框左側(cè)圖片
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Snip20160122_1"]];
pinView.leftCalloutAccessoryView = imageView;
return pinView;
``` - 自定義子類
- 注意事項(xiàng):如果