iOS原生地圖 MKMapView 庫翻譯

iOS原生地圖 MKMapView 庫翻譯

作業(yè)部落存檔

標(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末间唉,一起剝皮案震驚了整個濱河市绞灼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌呈野,老刑警劉巖低矮,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異被冒,居然都是意外死亡军掂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門昨悼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蝗锥,“玉大人率触,你說我怎么就攤上這事终议。” “怎么了葱蝗?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵穴张,是天一觀的道長。 經(jīng)常有香客問我两曼,道長皂甘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任悼凑,我火速辦了婚禮偿枕,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘佛析。我一直安慰自己益老,他們只是感情好彪蓬,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布寸莫。 她就那樣靜靜地躺著,像睡著了一般档冬。 火紅的嫁衣襯著肌膚如雪膘茎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天酷誓,我揣著相機(jī)與錄音披坏,去河邊找鬼。 笑死涂乌,一個胖子當(dāng)著我的面吹牛赔硫,可吹牛的內(nèi)容都是我干的莱坎。 我是一名探鬼主播倔撞,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼帚屉,長吁一口氣:“原來是場噩夢啊……” “哼谜诫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起攻旦,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤喻旷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后牢屋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體且预,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年烙无,在試婚紗的時候發(fā)現(xiàn)自己被綠了锋谐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡截酷,死狀恐怖怀估,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情合搅,我是刑警寧澤多搀,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站灾部,受9級特大地震影響康铭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赌髓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一从藤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锁蠕,春花似錦夷野、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至舌仍,卻和暖如春妒貌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背铸豁。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工灌曙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人节芥。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓在刺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蚣驼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容