IOS学习之——MapKit 地图的使用

来源:互联网 发布:php 获取pathinfo 编辑:程序博客网 时间:2024/05/17 23:56
MapKit框架使用前提导入框架导入主头文件#import <MapKit/MapKit.h>MapKit框架使用须知MapKit框架中所有数据类型的前缀都是MKMapKit有一个比较重要的UI控件 :MKMapView,专门用于地图显示跟踪显示用户的位置设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置MKUserTrackingModeNone :不跟踪用户的位置MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转下图是跟踪效果蓝色发光圆点就是用户的当前位置蓝色发光原点,专业术语叫做“大头针”可以通过设置MKMapView的mapViewType设置地图类型MKMapTypeStandard :普通地图(左图)MKMapTypeSatellite :卫星云图 (中图)MKMapTypeHybrid :普通地图覆盖于卫星云图之上(右图) 

这里写图片描述

MKMapView的代理MKMapView可以设置一个代理对象,用来监听地图的相关行为常见的代理方法有- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;一个位置更改默认只会调用一次,不断监测用户的当前位置每次调用,都会把用户的最新位置(userLocation参数)传进来- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;地图的显示区域即将发生改变的时候调用- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;地图的显示区域已经发生改变的时候调用MKUserLocationMKUserLocation其实是个大头针模型,包括以下属性@property (nonatomic, copy) NSString *title;显示在大头针上的标题@property (nonatomic, copy) NSString *subtitle;显示在大头针上的子标题@property (readonly, nonatomic) CLLocation *location;地理位置信息(大头针钉在什么地方?)

这里写图片描述

设置地图的显示通过MKMapView的下列方法,可以设置地图显示的位置和区域设置地图的中心点位置@property (nonatomic) CLLocationCoordinate2D centerCoordinate;- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;设置地图的显示区域@property (nonatomic) MKCoordinateRegion region;- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;MKCoordinateRegionMKCoordinateRegion是一个用来表示区域的结构体,定义如下typedef struct {                     CLLocationCoordinate2D center; // 区域的中心点位置        MKCoordinateSpan span; // 区域的跨度} MKCoordinateRegion;MKCoordinateSpan的定义typedef struct {    CLLocationDegrees latitudeDelta; // 纬度跨度    CLLocationDegrees longitudeDelta; // 经度跨度} MKCoordinateSpan;

这里写图片描述

大头针的基本操作添加一个大头针- (void)addAnnotation:(id <MKAnnotation>)annotation;添加多个大头针- (void)addAnnotations:(NSArray *)annotations;移除一个大头针- (void)removeAnnotation:(id <MKAnnotation>)annotation;移除多个大头针- (void)removeAnnotations:(NSArray *)annotations;(id <MKAnnotation>)annotation参数是什么东西?大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据大头针模型新建一个大头针模型类#import <MapKit/MapKit.h>@interface MyAnnotation : NSObject <MKAnnotation>/** 坐标位置 */@property (nonatomic, assign) CLLocationCoordinate2D coordinate;/** 标题 */@property (nonatomic, copy) NSString *title; /** 子标题 */@property (nonatomic, copy) NSString *subtitle; @end添加大头针MyAnnotation *anno = [[MyAnnotation alloc] init];anno.title = @"iOS学院";anno.subtitle = @"全部课程15折,会员20折,老学员30折";anno.coordinate = CLLocationCoordinate2DMake(40, 116);[self.mapView addAnnotation:anno];

这里写图片描述

自定义大头针如何自定义大头针设置MKMapView的代理实现下面的代理方法,返回大头针控件- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;根据传进来的(id <MKAnnotation>)annotation参数创建并返回对应的大头针控件代理方法的使用注意如果返回nil,显示出来的大头针就采取系统的默认样式标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法因此,需要在代理方法中分清楚(id <MKAnnotation>)annotation参数代表自定义的大头针还是蓝色发光圆点自定义大头针- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{    // 判断annotation的类型    if (![annotation isKindOfClass:[MJTuangouAnnotation class]]) return nil;    // 创建MKAnnotationView    static NSString *ID = @"tuangou";    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];    if (annoView == nil) {        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];        annoView.canShowCallout = YES;    }                     // 传递模型数据    annoView.annotation = annotation;    // 设置图片        MJTuangouAnnotation *tuangouAnnotation = annotation;    annoView.image = [UIImage imageNamed:tuangouAnnotation.icon];    return annoView;}MKAnnotationView地图上的大头针控件是MKAnnotationViewMKAnnotationView的属性@property (nonatomic, strong) id <MKAnnotation> annotation;大头针模型@property (nonatomic, strong) UIImage *image;显示的图片MKAnnotationView@property (nonatomic) BOOL canShowCallout;是否显示标注@property (nonatomic) CGPoint calloutOffset;标注的偏移量@property (strong, nonatomic) UIView *rightCalloutAccessoryView;标注右边显示什么控件@property (strong, nonatomic) UIView *leftCalloutAccessoryView;标注左边显示什么控件MKPinAnnotationViewMKPinAnnotationView是MKAnnotationView的子类MKPinAnnotationView比MKAnnotationView多了2个属性@property (nonatomic) MKPinAnnotationColor pinColor;大头针颜色@property (nonatomic) BOOL animatesDrop;大头针第一次显示时是否从天而降
0 0
原创粉丝点击