有時候做專案可能會用到地圖,比如在地圖上顯示附近的人,或在微博客戶端中顯示附近微博……所以我們有必要了解一下關於地圖視圖。
地圖視圖 MKMapView 繼承自 UIView。先來看一下屬性:
@property(nonatomic) MKMapType mapType //要顯示地圖的類型
enum {
MKMapTypeStandard, //標准地圖,顯示所有道路和路名
MKMapTypeSatellite, //顯示衛星視圖
MKMapTypeHybrid //混合顯示,將衛星視圖展示在標准地圖上
};
typedef NSUInteger MKMapType;
@property(nonatomic, getter=isZoomEnabled) BOOL zoomEnabled //是否使用捏合手勢
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled //是否可以滑動
@property(nonatomic, assign) id<MKMapViewDelegate> delegate //代理
@property(nonatomic) MKCoordinateRegion region //在 map view 中當前展示的區域
typedef struct {
CLLocationCoordinate2D center; //區域中心
MKCoordinateSpan span; //當前經度緯度的跨度,即地圖的縮放級別
} MKCoordinateRegion;
@property(nonatomic) CLLocationCoordinate2D centerCoordinate //map view 中心的坐標
@property(nonatomic) MKMapRect visibleMapRect //當前顯示在 map view 中的區域
typedef struct {
MKMapPoint origin;
MKMapSize size;
} MKMapRect;
@property(nonatomic) BOOL showsUserLocation //是否顯示用戶位置
@property(nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible //用戶的當前位置是否顯示在 map view 中,如果不能確定當前位置,則值為 NO
@property(nonatomic, readonly) MKUserLocation *userLocation //表示用戶當前位置的注釋對象
@property(nonatomic, readonly) NSArray *annotations //與接收對象相關聯的注釋列表(也就是通常我們說的大頭針)
@property(nonatomic, readonly) CGRect annotationVisibleRect //當前顯示的注釋視圖的可見矩形區域
@property(nonatomic, copy) NSArray *selectedAnnotations //當前被選定的注釋視圖(Annotation)
@property(nonatomic, readonly) NSArray *overlays //與當前 map view 相關聯的覆蓋層對象
@property(nonatomic) MKUserTrackingMode userTrackingMode //map view 跟蹤用戶位置的模式
enum {
MKUserTrackingModeNone = 0, //map view 不跟蹤用戶位置變化
MKUserTrackingModeFollow, //map view 跟隨用戶位置移動而移動
MKUserTrackingModeFollowWithHeading, //map view 跟隨用戶移動,並隨用戶方向變化而旋轉
};
typedef NSInteger MKUserTrackingMode;
下面是這個類的方法。
操作 map 的可視化部分 Manipulating the Visible Portion of the Map
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated //改變當前的可視化區域,其中參數 region 是將要展示的指定區域,是否使用動畫
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated //改變、設置 map 的中心坐標,是否使用動畫
- (void)setVisibleMapRect:(MKMapRect)mapRect animated:(BOOL)animate //改變當前地圖的可視化部分,顯示指定的地圖區域,並決定是否使用動畫
- (void)setVisibleMapRect:(MKMapRect)mapRect edgePadding:(UIEdgeInsets)insets animated:(BOOL)animate //改變當前地圖的可視化部分,並允許你在指定的邊緣增加空間
注釋 map Annotating the Map
- (void)addAnnotation:(id <MKAnnotation>)annotation //為地圖增加一個注釋對象
- (void)addAnnotations:(NSArray *)annotations //為地圖增加一組注釋對象
- (void)removeAnnotation:(id <MKAnnotation>)annotation //移除一個指定的注釋對象
- (void)removeAnnotations:(NSArray *)annotations //移除一組注釋對象
- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation //返回指定注釋對象相關聯的注釋視圖,如果視圖還未創建則返回 nil。要注意的是,如果注釋對象不在當前地圖可視區域內,那麼將返回 nil,因為注釋對象沒在可視區域內,就不能與注釋視圖相聯繫
- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect //返回處於指定地圖區域內的注釋對象
- (MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier //返回一個指定標識符的可重複利用的注釋視圖
管理注釋對象的選擇 Managing Annotation Selections
- (void)selectAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated //選擇指定的注釋對象,並為它顯示標注的視圖
- (void)deselectAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated //取消選定的注釋對象,並隱藏它調出的視圖
添加和移除覆蓋層 Adding and Removing Overlays
- (void)addOverlay:(id <MKOverlay>)overlay //添加單個覆蓋層對象到 map 中
- (void)addOverlays:(NSArray *)overlays //添加一組覆蓋層對象
- (void)removeOverlay:(id <MKOverlay>)overlay //移除一個覆蓋層對象
- (void)removeOverlays:(NSArray *)overlays //移除一組覆蓋層對象
- (void)insertOverlay:(id <MKOverlay>)overlay atIndex:(NSUInteger)index //在指定的位置插入一個覆蓋層對象
- (void)insertOverlay:(id <MKOverlay>)overlay aboveOverlay:(id <MKOverlay>)sibling //在指定的覆蓋層對象上面插入另一個覆蓋層對象
- (void)insertOverlay:(id <MKOverlay>)overlay belowOverlay:(id <MKOverlay>)sibling //在指定的覆蓋層對象下面插入另一個覆蓋層對象
- (void)exchangeOverlayAtIndex:(NSUInteger)index1 withOverlayAtIndex:(NSUInteger)index2 //交換指定位置上的兩個覆蓋層對象
- (MKOverlayView *)viewForOverlay:(id <MKOverlay>)overlay //返回與指定覆蓋層對象相關聯的視圖,如果覆蓋層不在顯示屏上則返回 nil
轉換地圖的坐標系 Converting Map Coordinates,也就是進行現實世界中的實際位置(經緯度)與視圖中區域或點的轉化
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view //將 map 中的一個坐標轉化為指定視圖中的一個點
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view //與上相反,將指定視圖中的一個點轉化為一個 map 中的坐標
- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(UIView *)view //將 map 中指定的部分區域轉化為指定視圖中的一個矩形區域
- (MKCoordinateRegion)convertRect:(CGRect)rect toRegionFromView:(UIView *)view //與上相反,將指定視圖中的一片區域轉化為 map 的一個部分區域
調整 map 的區域 Adjusting Map Regions and Rectangles
- (MKCoordinateRegion)regionThatFits:(MKCoordinateRegion)region //調整指定區域的縱橫比例,以便於適應 map 的視圖框架
- (MKMapRect)mapRectThatFits:(MKMapRect)mapRect //調整指定 map 的矩形區域的縱橫比例,以便於適應 map 的視圖框架
- (MKMapRect)mapRectThatFits:(MKMapRect)mapRect edgePadding:(UIEdgeInsets)insets //調整指定 map 的矩形區域的縱橫比例,並合併指定的插入值
typedef struct {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
跟蹤用戶位置 Tracking the User Location
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated //設置跟蹤用戶位置的模式
以上就是 MKMapView 這個類的所有內容了。當然由於還不熟悉,這其中可能會有不精準的地方,希望對你有所幫助。如果有什麼問題,請及時聯繫我。今天就到這裡 —— LC