IOS MKMapView 地圖視圖

有時候我們做項目可能會用到地圖,比如在地圖上顯示附近人,在微博客戶端中顯示附近微博。。。
所以我們有必要來了解一下關於地圖視圖

地圖視圖   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
發佈了56 篇原創文章 · 獲贊 4 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章