IOSMKMapView地图视图 详解

来源:互联网 发布:火炬之光mac版汉化教 编辑:程序博客网 时间:2024/05/16 15:53

http://www.it165.net/pro/html/201402/9319.html

  • 有时候我们做项目可能会用到地图,比如在地图上显示附近人,在微博客户端中显示附近微博。。。
    所以我们有必要来了解一下关于地图视图

    地图视图 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

0 0
原创粉丝点击