MapKit框架學習

  • MapKit是用來顯示地圖的.
  • MapKit的使用步驟
    • 導入框架MapKit
    • 導入頭文件#import <MapKit/MapKit.h>
    • storyboard中拖拽MKMapView
  • 地圖屬性的設置
    // 1. MKMapView的顯示項
    // 1.1 設置地圖的顯示樣式
    /*
     MKMapTypeStandard 標準
     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點
    self.mapView.showsPointsOfInterest = YES;
    
    // 1.5 顯示地圖上的縮放比例
    self.mapView.showsScale = YES;
    
    // 1.6 顯示地圖上的交通
    self.mapView.showsTraffic = YES;
    
    // 1.7 顯示用戶當前位置
    // 1.7.1 在iOS8.0之后要請求用戶授權
    [self locationManager];
    // 1.7.2 顯示用戶當前位置(如果不設置追蹤模式,地圖不會自動放大)
    self.mapView.showsUserLocation = YES;
    // 1.7.3 設置用戶的追蹤模式
    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    
    // 2. MKMapView的控制項
    // 2.1 地圖滾動
    self.mapView.scrollEnabled = YES;
    
    // 2.2 地圖縮放
    self.mapView.zoomEnabled = YES;
    
    // 2.3 地圖旋轉
    self.mapView.rotateEnabled = YES;
    
  • 地圖的使用-結合代理方法didUpdateUserLocationregionDidChangeAnimated
    // 1. 查看用戶當前位置信息
    NSLog(@"didUpdateUserLocation-------%f, %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    // 1.1 設置標題
    userLocation.title = @"qwer";
    // 1.2 設置子標題
    userLocation.subtitle = @"asdf";
    
    // 2. 調(diào)整地圖顯示中心
    // 2.1 獲取用戶的位置信息
    CLLocationCoordinate2D center = userLocation.location.coordinate;
    // 2.2 設置地圖的中心為用戶的位置
    [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 設置地圖顯示區(qū)域為創(chuàng)建的區(qū)域
    [self.mapView setRegion:region animated:YES];
    
  • 大頭針的基本使用
    • 實現(xiàn)步驟
      • 創(chuàng)建地圖
      • 定義大頭針數(shù)據(jù)模型類
      • 創(chuàng)建大頭針數(shù)據(jù)模型
      • 添加大頭針數(shù)據(jù)模型
    • 代碼實現(xiàn)
      /* 大頭針數(shù)據(jù)模型類 */
      @interface ZQAnnotation : NSObject <MKAnnotation>
      /** 經(jīng)緯度 */
      @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
      /** 彈框標題 */
      @property (nonatomic, copy, nullable) NSString *title;
      /** 彈框子標題 */
      @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ù)據(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];
      
  • 大頭針使用場景案例
    • 實現(xiàn)步驟
      • 創(chuàng)建地圖
      • 自定義大頭針數(shù)據(jù)模型
      • 獲取手指所在的點
      • 添加大頭針數(shù)據(jù)模型
      • 標注彈框
    • 關鍵代碼
      /*
      場景描述:鼠標點擊在地圖哪個位置,
      就在對應的位置添加一個大頭針,
      并在標注彈框中顯示對應的城市和街道
      */
      

    pragma mark ------------------

pragma mark - Events

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

// 1. 獲取手指所在的點
CGPoint touch = [[touches anyObject] locationInView:self.mapView];

// 2. 把點轉化為經(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) {

        // 從地標數(shù)組中獲取地標對象
        CLPlacemark *placemark = [placemarks firstObject];

        // 設置彈框標題
        annotationItem.title = placemark.name;

        // 設置彈框子標題
        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ù)據(jù)
    annotationItem.coordinate = coordinate;
    annotationItem.title = title;
    annotationItem.subtitle = subTitle;

    // 添加大頭針數(shù)據(jù)模型
    [self.mapView addAnnotation:annotationItem];

    return annotationItem;
}
        ```
* 知識點
    * 獲取手指所在的點
    * 把點轉化為經(jīng)緯度
    * 反地理編碼
  • 自定義大頭針
    • 注意事項:如果viewForAnnotation這個方法沒有實現(xiàn),或者, 返回nil, 那么系統(tǒng)就會調(diào)用默認的大頭針視圖
    • 理論基礎:如果想要自定義大頭針,要么是用父類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;
      // 設置彈框
      pinView.canShowCallout = YES;
      // 設置大頭針圖片
      pinView.image = [UIImage imageNamed:@"category_3"];
      // 設置彈框左側圖片
      UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Snip20160122_1"]];
      pinView.leftCalloutAccessoryView = imageView;
      return pinView;
      ```
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容