iOS原生地圖 MKMapView 庫(kù)翻譯
標(biāo)簽: iOS地圖
- 引入系統(tǒng)庫(kù)
- @import MapKit; 地圖
- @import CoreLocation;定位
MKMapView
- mapType ---地圖類(lèi)型
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 , - 是一個(gè)用來(lái)表示區(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ū)域
這里會(huì)更改地圖的縮放等級(jí)
如不想更改縮放等級(jí) 可以用setCenterCoordinate
- (MKCoordinateRegion)regionThatFits:(MKCoordinateRegion)region;
某些情況下,我們要調(diào)整region ,為了更好的顯示在手機(jī)屏幕上
CLLocationCoordinate2D centerCoordinate - 地圖的中心點(diǎn)
MKMapRect visibleMapRect; 第二方法 種創(chuàng)建地圖顯示范圍 ,一塊矩形范圍 常常用于地圖截圖屬性size賦值
mapRectThatFits 同 regionThatFits
MKMapCamera *camera
3D視角 類(lèi)似于地圖街景,增強(qiáng)用戶(hù)體驗(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;
- 是否顯示羅盤(pán)(指南針)
@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
- 是否獲取用戶(hù)當(dāng)前位置信息
@property (nonatomic) BOOL showsUserLocation;
- 用戶(hù)大頭針,不能直接實(shí)例化,可用map.userLocation獲得
@property (nonatomic, readonly) MKUserLocation *userLocation;
- 設(shè)置用戶(hù)定位模式
@property (nonatomic) MKUserTrackingMode userTrackingMode NS_AVAILABLE(NA, 5_0);
MKUserTrackingMode 枚舉:
MKUserTrackingModeNone 不定位
MKUserTrackingModeFollow 定位
MKUserTrackingModeFollowWithHeading 定位并且顯示方向
- 用戶(hù)坐標(biāo)是否可見(jiàn)
@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)注都會(huì)觸發(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;
- 獲取某個(gè)范圍內(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沒(méi)有顯示在map上時(shí) 返回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 同時(shí)出發(fā)代理方法
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
如果此時(shí)標(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)前顯示的注釋視圖的可見(jiàn)的矩形區(qū)域
@property (nonatomic, readonly) CGRect annotationVisibleRect;
在代理方法mapView:didAddAnnotationViews:
中,可以做添加標(biāo)注view動(dòng)畫(huà),這個(gè)時(shí)候會(huì)用到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ù)不同的覆蓋物類(lèi)型獲取 不同的覆蓋物集合
// 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) 覆蓋物還沒(méi)有展示在屏幕上時(shí),返回nil
//根據(jù)覆蓋物返回 渲染器, 額,看了下,渲染器這個(gè)東西貌似很屌的感覺(jué)
#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 類(lèi)型的覆蓋物,蘋(píng)果不建議用這個(gè)方法
- (void)addOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 4_0);
//添加多個(gè)覆蓋物
- (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;
//開(kāi)始渲染地圖元素
- (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 時(shí),返回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.
//在這個(gè)方法中給 標(biāo)注view添加 展示動(dòng)畫(huà)
- (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.
//這個(gè)方法中,當(dāng)用戶(hù)點(diǎn)擊了標(biāo)注view 左邊或者右邊的view(具有點(diǎn)擊事件,要提前添加)時(shí),調(diào)用事件方法。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control __TVOS_PROHIBITED;
#endif
//點(diǎn)擊 彈出標(biāo)注view時(shí)的代理
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//彈出的 標(biāo)注view收回的時(shí)候 調(diào)用
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//將要開(kāi)始定位用戶(hù)位置前的方法
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView NS_AVAILABLE(10_9, 4_0);
//用戶(hù)位置定位結(jié)束 后
- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView NS_AVAILABLE(10_9, 4_0);
//更新用戶(hù)位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation NS_AVAILABLE(10_9, 4_0);
//用戶(hù)位置定位失敗
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error NS_AVAILABLE(10_9, 4_0);
//標(biāo)注view 被拖拽的時(shí)候調(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
//用戶(hù)定位模式 更改時(shí)的方法
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated NS_AVAILABLE(NA, 5_0);
#endif
//給地圖添加遮蓋物時(shí)候,必須調(diào)用 [使用方法](http://www.wtoutiao.com/p/3d49ULc.html)
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay NS_AVAILABLE(10_9, 7_0);
//添加多個(gè)遮蓋物結(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