iOS開發之MapKit

來源:互聯網
上載者:User

標籤:

MapKit架構的使用

匯入架構

 

匯入主標頭檔

#import <MapKit/MapKit.h>

MapKit架構使用須知

MapKit架構中所有資料類型的首碼都是MK

MapKit有一個比較重要的UI控制項 :MKMapView,專門用於地圖顯示

跟蹤顯示使用者的位置

設定MKMapView的userTrackingMode屬性可以跟蹤顯示使用者的當前位置

MKUserTrackingModeNone :不跟蹤使用者的位置

MKUserTrackingModeFollow :跟蹤並在地圖上顯示使用者的當前位置

MKUserTrackingModeFollowWithHeading :跟蹤並在地圖上顯示使用者的當前位置,地圖會跟隨使用者的前進方向進行旋轉

 

是跟蹤效果

藍色發光圓點就是使用者的當前位置

藍色發光原點,專業術語叫做“圖釘”

 

地圖的類型

可以通過設定MKMapView的mapViewType設定地圖類型

MKMapTypeStandard :普通地圖(左圖)

MKMapTypeSatellite :衛星雲圖 (中圖)

MKMapTypeHybrid :普通地圖覆蓋於衛星雲圖之上(右圖) 

                

 

MKMapView的代理

MKMapView可以設定一個代理對象,用來監聽地圖的相關行為

 

常見的代理方法有

 1 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation; 

調用非常頻繁,不斷監測使用者的當前位置

每次調用,都會把使用者的最新位置(userLocation參數)傳進來

 1 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated; 

地圖的顯示地區即將發生改變的時候調用

 1 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; 

地圖的顯示地區已經發生改變的時候調用

MKUserLocation

MKUserLocation其實是個圖釘模型,包括以下屬性

 1 @property (nonatomic, copy) NSString *title; 

顯示在圖釘上的標題

 1 @property (nonatomic, copy) NSString *subtitle; 

顯示在圖釘上的子標題

 1 @property (readonly, nonatomic) CLLocation *location; 

地理位置資訊(圖釘釘在什麼地方?)

設定地圖的顯示

通過MKMapView的下列方法,可以設定地圖顯示的位置和地區

設定地圖的中心點位置

1 @property (nonatomic) CLLocationCoordinate2D centerCoordinate;2 3 - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;

設定地圖的顯示地區

1 @property (nonatomic) MKCoordinateRegion region;2 3 - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
MKCoordinateRegion

MKCoordinateRegion是一個用來表示地區的結構體,定義如下

1 typedef struct {2 3       CLLocationCoordinate2D center; // 地區的中心點位置4 5       MKCoordinateSpan span; // 地區的跨度6 7 } MKCoordinateRegion;

MKCoordinateSpan的定義

1 typedef struct {2 3     CLLocationDegrees latitudeDelta; // 緯度跨度4 5     CLLocationDegrees longitudeDelta; // 經度跨度6 7 } MKCoordinateSpan;
圖釘

什麼是圖釘

現實生活中的圖釘

地圖上的圖釘

釘在某個具體位置,用來標識這個位置上有特定的事物(比如這個位置上有家餐館)

圖釘的基本操作

添加一個圖釘

- (void)addAnnotation:(id <MKAnnotation>)annotation;

添加多個圖釘

- (void)addAnnotations:(NSArray *)annotations;

移除一個圖釘

- (void)removeAnnotation:(id <MKAnnotation>)annotation;

 移除多個圖釘

- (void)removeAnnotations:(NSArray *)annotations;

(id <MKAnnotation>)annotation參數是什麼東西?

圖釘模型對象:用來封裝圖釘的資料,比如圖釘的位置、標題、子標題等資料

圖釘模型

建立一個圖釘模型類

 1 #import <MapKit/MapKit.h> 2  3 @interface MJTuangouAnnotation : NSObject <MKAnnotation> 4 /** 座標位置 */ 5 @property (nonatomic, assign) CLLocationCoordinate2D coordinate; 6 /** 標題 */ 7 @property (nonatomic, copy) NSString *title;  8 /** 子標題 */ 9 @property (nonatomic, copy) NSString *subtitle; 10 @end

添加圖釘

1 MJTuangouAnnotation *anno = [[MJTuangouAnnotation alloc] init];2 anno.title = @"你怎麼看";3 anno.subtitle = @"我趴窗戶上看";4 anno.coordinate = CLLocationCoordinate2DMake(40, 116);5 [self.mapView addAnnotation:anno];

自訂圖釘

很多情況下,需要自訂圖釘的顯示樣式,比如顯示一張圖片

 

自訂圖釘

如何自訂圖釘

設定MKMapView的代理

實現下面的代理方法,返回圖釘控制項

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

根據傳進來的(id <MKAnnotation>)annotation參數建立並返回對應的圖釘控制項

 

代理方法的使用注意

如果返回nil,顯示出來的圖釘就採取系統的預設樣式

標識使用者位置的藍色發光圓點,它也是一個圖釘,當顯示這個圖釘時,也會調用代理方法

因此,需要在代理方法中分清楚(id <MKAnnotation>)annotation參數代表自訂的圖釘還是藍色發光圓點

 1 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 2 { 3     // 判斷annotation的類型 4     if (![annotation isKindOfClass:[MJTuangouAnnotation class]]) return nil; 5      6     // 建立MKAnnotationView 7     static NSString *ID = @"tuangou"; 8     MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID]; 9     if (annoView == nil) {10         annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];11         annoView.canShowCallout = YES;12     }13     // 傳遞模型資料14     annoView.annotation = annotation;15     16     // 設定圖片17         MJTuangouAnnotation *tuangouAnnotation = annotation;18     annoView.image = [UIImage imageNamed:tuangouAnnotation.icon];19     20     return annoView;21 }
MKAnnotationView

地圖上的圖釘控制項是MKAnnotationView

 MKAnnotationView的屬性

 1 @property (nonatomic, strong) id <MKAnnotation> annotation; 2 圖釘模型 3  4 @property (nonatomic, strong) UIImage *image; 5 顯示的圖片 6  7 @property (nonatomic) BOOL canShowCallout; 8 是否顯示標註 9 10 @property (nonatomic) CGPoint calloutOffset;11 標註的位移量12 13 @property (strong, nonatomic) UIView *rightCalloutAccessoryView;14 標註右邊顯示什麼控制項15 16 @property (strong, nonatomic) UIView *leftCalloutAccessoryView;17 標註左邊顯示什麼控制項
MKPinAnnotationView

MKPinAnnotationView是MKAnnotationView的子類

MKPinAnnotationView比MKAnnotationView多了2個屬性

1 @property (nonatomic) MKPinAnnotationColor pinColor;2 圖釘顏色3 4 @property (nonatomic) BOOL animatesDrop;5 圖釘第一次顯示時是否從天而降

 

iOS開發之MapKit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.