iOS原生地图 MKMapView 库翻译
作业部落存档
标签: iOS地图
- 引入系统库
- @import MapKit; 地图
- @import CoreLocation;定位
MKMapView
- mapType ---地图类型
MKMapTypeStandard = 0, // 标准
MKMapTypeSatellite, // 卫星
MKMapTypeHybrid, // 混合(标准+卫星)
MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立体卫星
MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立体混合
- MKCoordinateRegion region , - 是一个用来表示区域的结构体,定义如下:
typedef struct {
CLLocationCoordinate2D center; // 区域的中心点位置
MKCoordinateSpan span; // 区域的跨度
} MKCoordinateRegion;
typedef struct {
CLLocationDegrees latitudeDelta; // 纬度跨度
CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
设置地图显示区域
这里会更改地图的缩放等级
如不想更改缩放等级 可以用setCenterCoordinate
- (MKCoordinateRegion)regionThatFits:(MKCoordinateRegion)region;
某些情况下,我们要调整region ,为了更好的显示在手机屏幕上
CLLocationCoordinate2D centerCoordinate - 地图的中心点
MKMapRect visibleMapRect; 第二方法 种创建地图显示范围 ,一块矩形范围 常常用于地图截图属性size赋值
mapRectThatFits 同 regionThatFits
MKMapCamera *camera
3D视角 类似于地图街景,增强用户体验把地图坐标转化为view上的point
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable UIView *)view;
- 把view上的point 转化为地图上的坐标点
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(nullable UIView *)view;
- 地图显示区域转化为view上的矩形范围
- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(nullable UIView *)view;
- view上的矩形范围抓化为地图上的显示区域
- (MKCoordinateRegion)convertRect:(CGRect)rect toRegionFromView:(nullable UIView *)view;
- 是否可以缩放
@property (nonatomic, getter=isZoomEnabled) BOOL zoomEnabled;
- 是否可以拖拽
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
- 是否可以旋转
@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
- 是否获取用户当前位置信息
@property (nonatomic) BOOL showsUserLocation;
- 用户大头针,不能直接实例化,可用map.userLocation获得
@property (nonatomic, readonly) MKUserLocation *userLocation;
- 设置用户定位模式
@property (nonatomic) MKUserTrackingMode userTrackingMode NS_AVAILABLE(NA, 5_0);
MKUserTrackingMode 枚举:
MKUserTrackingModeNone 不定位
MKUserTrackingModeFollow 定位
MKUserTrackingModeFollowWithHeading 定位并且显示方向
- 用户坐标是否可见
@property (nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible;
Returns YES if the user's location is displayed within the currently visible map region.
- 添加标注
annotations 是地图标注的模型
每次添加标注都会触发代理mapView:viewForAnnotation:
得到标注view MKAnnotationView
- (void)addAnnotation:(id
)annotation;
- (void)addAnnotations:(NSArray
> *)annotations;
- 移除标注
- (void)removeAnnotation:(id
)annotation;
- (void)removeAnnotations:(NSArray
> *)annotations;
- 获取所有的标注
@property (nonatomic, readonly) NSArray
> *annotations;
- 获取某个范围内所有的标注
- (NSSet
> *)annotationsInMapRect:(MKMapRect)mapRect NS_AVAILABLE(10_9, 4_2);
- 根据特定的annotation 获取标注view 当annotation 为空,或者标注view没有显示在map上时 返回nil
- (nullable MKAnnotationView *)viewForAnnotation:(id
)annotation;
- 从缓存池中查找指定ID的自定义大头针模型 主要用于代理
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id
中,创建标注view)annotation
- (nullable MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;
- 代码选中标注view 同时出发代理方法
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
如果此时标注view在屏幕外面,则此方法失效
- (void)selectAnnotation:(id
)annotation animated:(BOOL)animated;
- 代码取消选中标注view 触发代理
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
- (void)deselectAnnotation:(nullable id
)annotation animated:(BOOL)animated;
- 获取所有选中的标注
@property (nonatomic, copy) NSArray
> *selectedAnnotations;
- 当前显示的注释视图的可见的矩形区域
@property (nonatomic, readonly) CGRect annotationVisibleRect;
在代理方法mapView:didAddAnnotationViews:
中,可以做添加标注view动画,这个时候会用到annotationVisibleRect
点击查看demo
- 尽可能的显示所有的标注view
- (void)showAnnotations:(NSArray
> *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0);
@interface MKMapView (OverlaysAPI)
- 添加覆盖物 删除覆盖物 插入覆盖物 交换覆盖物
typedef NS_ENUM(NSInteger, MKOverlayLevel) {
MKOverlayLevelAboveRoads = 0, //覆盖物位于道路之上
MKOverlayLevelAboveLabels//覆盖物位于标签之上
} NS_ENUM_AVAILABLE(10_9, 7_0) __TVOS_AVAILABLE(9_2) __WATCHOS_PROHIBITED;
- (void)addOverlay:(id )overlay level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)addOverlays:(NSArray> *)overlays level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)removeOverlay:(id )overlay NS_AVAILABLE(10_9, 4_0);
- (void)removeOverlays:(NSArray> *)overlays NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id )overlay atIndex:(NSUInteger)index level:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);
- (void)insertOverlay:(id )overlay aboveOverlay:(id )sibling NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id )overlay belowOverlay:(id )sibling NS_AVAILABLE(10_9, 4_0);
- (void)exchangeOverlay:(id )overlay1 withOverlay:(id )overlay2 NS_AVAILABLE(10_9, 7_0);
@property (nonatomic, readonly) NSArray> *overlays NS_AVAILABLE(10_9, 4_0);//地图上所有的覆盖物
- (NSArray> *)overlaysInLevel:(MKOverlayLevel)level NS_AVAILABLE(10_9, 7_0);//根据不同的覆盖物类型获取 不同的覆盖物集合
// Current renderer for overlay; returns nil if the overlay is not shown.
- (nullable MKOverlayRenderer *)rendererForOverlay:(id )overlay NS_AVAILABLE(10_9, 7_0);
//当 覆盖物还没有展示在屏幕上时,返回nil
//根据覆盖物返回 渲染器, 额,看了下,渲染器这个东西貌似很屌的感觉
#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 )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.
//默认添加MKOverlayLevelAboveLabels 类型的覆盖物,苹果不建议用这个方法
- (void)addOverlay:(id )overlay NS_AVAILABLE(10_9, 4_0);
//添加多个覆盖物
- (void)addOverlays:(NSArray> *)overlays NS_AVAILABLE(10_9, 4_0);
- (void)insertOverlay:(id )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
//地图显示区域将要改变
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
//地图显示区域已经改变
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
//地图将要加载
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView;
//地图已经加载完毕
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView;
//地图加载失败
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error;
//开始渲染地图元素
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView NS_AVAILABLE(10_9, 7_0);
//结束渲染地图元素
- (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.
//当使用系统提供的annotation 时,返回nil
//下面的方法只要用于自定义的annotation
//根据annotation 在地图上展示标注view
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )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.
//在这个方法中给 标注view添加 展示动画
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
#if TARGET_OS_IPHONE
// mapView:annotationView:calloutAccessoryControlTapped: is called when the user taps on left & right callout accessory UIControls.
//这个方法中,当用户点击了标注view 左边或者右边的view(具有点击事件,要提前添加)时,调用事件方法。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control __TVOS_PROHIBITED;
#endif
//点击 弹出标注view时的代理
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//弹出的 标注view收回的时候 调用
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(10_9, 4_0);
//将要开始定位用户位置前的方法
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView NS_AVAILABLE(10_9, 4_0);
//用户位置定位结束 后
- (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);
//标注view 被拖拽的时候调用
- (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
//给地图添加遮盖物时候,必须调用 [使用方法](http://www.wtoutiao.com/p/3d49ULc.html)
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id )overlay NS_AVAILABLE(10_9, 7_0);
//添加多个遮盖物结束后调用
- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers NS_AVAILABLE(10_9, 7_0);
#if TARGET_OS_IPHONE
// Prefer -mapView:rendererForOverlay:
// 获取覆盖物
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )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