iOS原生地圖 MKMapView 庫翻譯
標(biāo)簽: iOS地圖
- 引入系統(tǒng)庫
- @import MapKit; 地圖
- @import CoreLocation;定位
MKMapView
- mapType ---地圖類型
MKMapTypeStandard = 0, // 標(biāo)準(zhǔn)
MKMapTypeSatellite, // 衛(wèi)星
MKMapTypeHybrid, // 混合(標(biāo)準(zhǔn)+衛(wèi)星)
MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立體衛(wèi)星
MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立體混合
- MKCoordinateRegion region , - 是一個用來表示區(qū)域的結(jié)構(gòu)體镐躲,定義如下:
typedef struct {
CLLocationCoordinate2D center; // 區(qū)域的中心點(diǎn)位置
MKCoordinateSpan span; // 區(qū)域的跨度
} MKCoordinateRegion;
typedef struct {
CLLocationDegrees latitudeDelta; // 緯度跨度
CLLocationDegrees longitudeDelta; // 經(jīng)度跨度
} MKCoordinateSpan;
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
設(shè)置地圖顯示區(qū)域
這里會更改地圖的縮放等級
如不想更改縮放等級 可以用setCenterCoordinate
- (MKCoordinateRegion)regionThatFits:(MKCoordinateRegion)region;
某些情況下,我們要調(diào)整region 匣椰,為了更好的顯示在手機(jī)屏幕上
CLLocationCoordinate2D centerCoordinate - 地圖的中心點(diǎn)
MKMapRect visibleMapRect; 第二方法 種創(chuàng)建地圖顯示范圍 禽笑,一塊矩形范圍 常常用于地圖截圖屬性size賦值
mapRectThatFits 同 regionThatFits
MKMapCamera *camera
3D視角 類似于地圖街景蒲每,增強(qiáng)用戶體驗(yàn)把地圖坐標(biāo)轉(zhuǎn)化為view上的point
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable UIView *)view;
- 把view上的point 轉(zhuǎn)化為地圖上的坐標(biāo)點(diǎn)
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(nullable UIView *)view;
- 地圖顯示區(qū)域轉(zhuǎn)化為view上的矩形范圍
- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(nullable UIView *)view;
- view上的矩形范圍抓化為地圖上的顯示區(qū)域
- (MKCoordinateRegion)convertRect:(CGRect)rect toRegionFromView:(nullable UIView *)view;
- 是否可以縮放
@property (nonatomic, getter=isZoomEnabled) BOOL zoomEnabled;
- 是否可以拖拽
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
- 是否可以旋轉(zhuǎn)
@property (nonatomic, getter=isRotateEnabled) BOOL rotateEnabled NS_AVAILABLE(10_9, 7_0) __TVOS_PROHIBITED;
- 是否顯示3DVIEW
@property (nonatomic, getter=isPitchEnabled) BOOL pitchEnabled NS_AVAILABLE(10_9, 7_0) __TVOS_PROHIBITED;
- 是否顯示羅盤(指南針)
@property (nonatomic) BOOL showsCompass NS_AVAILABLE(10_9, 9_0) __TVOS_PROHIBITED;
- 是否可以縮放
@property (nonatomic) BOOL showsScale NS_AVAILABLE(10_10, 9_0);
- 是否顯示比例尺
@property (nonatomic) BOOL showsPointsOfInterest NS_AVAILABLE(10_9, 7_0); // Affects MKMapTypeStandard and MKMapTypeHybrid
- 是否顯示建筑物
@property (nonatomic) BOOL showsBuildings NS_AVAILABLE(10_9, 7_0); // Affects MKMapTypeStandard
- 是否顯示路況
@property (nonatomic) BOOL showsTraffic NS_AVAILABLE(10_11, 9_0); // Affects MKMapTypeStandard and MKMapTypeHybrid
- 是否獲取用戶當(dāng)前位置信息
@property (nonatomic) BOOL showsUserLocation;
- 用戶大頭針唬血,不能直接實(shí)例化拷恨,可用map.userLocation獲得
@property (nonatomic, readonly) MKUserLocation *userLocation;
- 設(shè)置用戶定位模式
@property (nonatomic) MKUserTrackingMode userTrackingMode NS_AVAILABLE(NA, 5_0);
MKUserTrackingMode 枚舉:
MKUserTrackingModeNone 不定位
MKUserTrackingModeFollow 定位
MKUserTrackingModeFollowWithHeading 定位并且顯示方向
- 用戶坐標(biāo)是否可見
@property (nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible;
Returns YES if the user's location is displayed within the currently visible map region.
- 添加標(biāo)注
annotations 是地圖標(biāo)注的模型
每次添加標(biāo)注都會觸發(fā)代理mapView:viewForAnnotation:
得到標(biāo)注view MKAnnotationView
- (void)addAnnotation:(id <MKAnnotation>)annotation;
- (void)addAnnotations:(NSArray<id<MKAnnotation>> *)annotations;
- 移除標(biāo)注
- (void)removeAnnotation:(id <MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray<id<MKAnnotation>> *)annotations;
- 獲取所有的標(biāo)注
@property (nonatomic, readonly) NSArray<id<MKAnnotation>> *annotations;
- 獲取某個范圍內(nèi)所有的標(biāo)注
- (NSSet<id<MKAnnotation>> *)annotationsInMapRect:(MKMapRect)mapRect NS_AVAILABLE(10_9, 4_2);
- 根據(jù)特定的annotation 獲取標(biāo)注view 當(dāng)annotation 為空小泉,或者標(biāo)注view沒有顯示在map上時 返回nil
- (nullable MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation;
- 從緩存池中查找指定ID的自定義大頭針模型 主要用于代理
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
中微姊,創(chuàng)建標(biāo)注view
- (nullable MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;
- 代碼選中標(biāo)注view 同時出發(fā)代理方法
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
如果此時標(biāo)注view在屏幕外面兢交,則此方法失效
- (void)selectAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated;
- 代碼取消選中標(biāo)注view 觸發(fā)代理
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
- (void)deselectAnnotation:(nullable id <MKAnnotation>)annotation animated:(BOOL)animated;
- 獲取所有選中的標(biāo)注
@property (nonatomic, copy) NSArray<id<MKAnnotation>> *selectedAnnotations;
- 當(dāng)前顯示的注釋視圖的可見的矩形區(qū)域
@property (nonatomic, readonly) CGRect annotationVisibleRect;
在代理方法mapView:didAddAnnotationViews:
中配喳,可以做添加標(biāo)注view動畫晴裹,這個時候會用到annotationVisibleRect
點(diǎn)擊查看demo
- 盡可能的顯示所有的標(biāo)注view
- (void)showAnnotations:(NSArray<id<MKAnnotation>> *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0);
@interface MKMapView (OverlaysAPI)
- 添加覆蓋物 刪除覆蓋物 插入覆蓋物 交換覆蓋物
typedef NS_ENUM(NSInteger, MKOverlayLevel) {
MKOverlayLevelAboveRoads = 0, //覆蓋物位于道路之上
MKOverlayLevelAboveLabels//覆蓋物位于標(biāo)簽之上
} NS_ENUM_AVAILABLE(10_9, 7_0) __TVOS_AVAILABLE(9_2) __WATCHOS_PROHIBITED;
- (void)addOverlay:(id <MKOverlay>)overlay level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)addOverlays:(NSArray<id<MKOverlay>> *)overlays level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)removeOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 4_0);
- (void)removeOverlays:(NSArray<id<MKOverlay>> *)overlays NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id <MKOverlay>)overlay atIndex:(NSUInteger)index level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)insertOverlay:(id <MKOverlay>)overlay aboveOverlay:(id <MKOverlay>)sibling NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id <MKOverlay>)overlay belowOverlay:(id <MKOverlay>)sibling NS_AVAILABLE(10_9, 4_0);
- (void)exchangeOverlay:(id <MKOverlay>)overlay1 withOverlay:(id <MKOverlay>)overlay2 NS_AVAILABLE(10_9, 7_0);
@property (nonatomic, readonly) NSArray<id<MKOverlay>> *overlays NS_AVAILABLE(10_9, 4_0);//地圖上所有的覆蓋物
- (NSArray<id<MKOverlay>> *)overlaysInLevel:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);//根據(jù)不同的覆蓋物類型獲取 不同的覆蓋物集合
// Current renderer for overlay; returns nil if the overlay is not shown.
- (nullable MKOverlayRenderer *)rendererForOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 7_0);
//當(dāng) 覆蓋物還沒有展示在屏幕上時溉潭,返回nil
//根據(jù)覆蓋物返回 渲染器喳瓣, 額畏陕,看了下仿滔,渲染器這個東西貌似很屌的感覺
#if TARGET_OS_IPHONE
// Currently displayed view for overlay; returns nil if the view has not been created yet.
// Prefer using MKOverlayRenderer and -rendererForOverlay.
//返回覆蓋物view
- (MKOverlayView *)viewForOverlay:(id <MKOverlay>)overlay NS_DEPRECATED_IOS(4_0, 7_0) __TVOS_PROHIBITED;
#endif
// These methods operate implicitly on overlays in MKOverlayLevelAboveLabels and may be deprecated in a future release in favor of the methods that specify the level.
//默認(rèn)添加MKOverlayLevelAboveLabels 類型的覆蓋物鞠绰,蘋果不建議用這個方法
- (void)addOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 4_0);
//添加多個覆蓋物
- (void)addOverlays:(NSArray<id<MKOverlay>> *)overlays NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id <MKOverlay>)overlay atIndex:(NSUInteger)index NS_AVAILABLE(10_9, 4_0);
- (void)exchangeOverlayAtIndex:(NSUInteger)index1 withOverlayAtIndex:(NSUInteger)index2 NS_AVAILABLE(10_9, 4_0);
@protocol MKMapViewDelegate <NSObject>
//地圖顯示區(qū)域?qū)⒁淖?- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
//地圖顯示區(qū)域已經(jīng)改變
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
//地圖將要加載
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
//地圖已經(jīng)加載完畢
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;
//地圖加載失敗
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error;
//開始渲染地圖元素
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView NS_AVAILABLE(10_9, 7_0);
//結(jié)束渲染地圖元素
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered NS_AVAILABLE(10_9, 7_0);
// mapView:viewForAnnotation: provides the view for each annotation.
// This method may be called for all or some of the added annotations.
// For MapKit provided annotations (eg. MKUserLocation) return nil to use the MapKit provided annotation view.
//當(dāng)使用系統(tǒng)提供的annotation 時蜈膨,返回nil
//下面的方法只要用于自定義的annotation
//根據(jù)annotation 在地圖上展示標(biāo)注view
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
// mapView:didAddAnnotationViews: is called after the annotation views have been added and positioned in the map.
// The delegate can implement this method to animate the adding of the annotations views.
// Use the current positions of the annotation views as the destinations of the animation.
//在這個方法中給 標(biāo)注view添加 展示動畫
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views;
#if TARGET_OS_IPHONE
// mapView:annotationView:calloutAccessoryControlTapped: is called when the user taps on left & right callout accessory UIControls.
//這個方法中翁巍,當(dāng)用戶點(diǎn)擊了標(biāo)注view 左邊或者右邊的view(具有點(diǎn)擊事件灶壶,要提前添加)時驰凛,調(diào)用事件方法担扑。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control __TVOS_PROHIBITED;
#endif
//點(diǎn)擊 彈出標(biāo)注view時的代理
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//彈出的 標(biāo)注view收回的時候 調(diào)用
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//將要開始定位用戶位置前的方法
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView NS_AVAILABLE(10_9, 4_0);
//用戶位置定位結(jié)束 后
- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView NS_AVAILABLE(10_9, 4_0);
//更新用戶位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation NS_AVAILABLE(10_9, 4_0);
//用戶位置定位失敗
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error NS_AVAILABLE(10_9, 4_0);
//標(biāo)注view 被拖拽的時候調(diào)用
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState NS_AVAILABLE(10_9, 4_0) __TVOS_PROHIBITED;
#if TARGET_OS_IPHONE
//用戶定位模式 更改時的方法
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated NS_AVAILABLE(NA, 5_0);
#endif
//給地圖添加遮蓋物時候渔隶,必須調(diào)用 [使用方法](http://www.wtoutiao.com/p/3d49ULc.html)
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 7_0);
//添加多個遮蓋物結(jié)束后調(diào)用
- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray<MKOverlayRenderer *> *)renderers NS_AVAILABLE(10_9, 7_0);
#if TARGET_OS_IPHONE
// Prefer -mapView:rendererForOverlay:
// 獲取覆蓋物
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay NS_DEPRECATED_IOS(4_0, 7_0) __TVOS_PROHIBITED;
// Called after the provided overlay views have been added and positioned in the map.
// Prefer -mapView:didAddOverlayRenderers:
- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews NS_DEPRECATED_IOS(4_0, 7_0) __TVOS_PROHIBITED;
#endif
@end
NS_ASSUME_NONNULL_END