iOS地图----MapKit框架
2015-08-02 23:14
435 查看
1.MapKit框架使用前提
①导入框架
②导入主头文件
#import <MapKit/MapKit.h>
③MapKit框架使用须知
MapKit框架中所有数据类型的前缀都是MKMapKit有一个比较重要的UI控件,专门用于地图显示
④MapKit框架包含CLLocation
2.设置地图的类型
可以通过设置MKMapView的mapType设置地图类型
typedef enum : NSUInteger { MKMapTypeStandard , 标准(默认) MKMapTypeSatellite ,卫星 MKMapTypeHybrid 混合(标准 + 卫星) } MKMapType;
[/code]
3.追踪用户的位置,地图放大显示
注意:在iOS8中, 如果想要追踪用户的位置, 必须自己主动请求隐私权限
在CLLocation框架中CLLocationManager请求授权
利用MapKit获取用户的位置, 可以追踪
userTrackingMode
/* typedef NS_ENUM(NSInteger, MKUserTrackingMode) { MKUserTrackingModeNone = 0, 不追踪/不准确的 MKUserTrackingModeFollow, 追踪 MKUserTrackingModeFollowWithHeading, 追踪并且获取用的方向 } */ self.mapView.userTrackingMode = MKUserTrackingModeFollow;
4.设代理,实现代理方法
self.mapView.delegate = self;
代理方法:
①地图的区域改变完成时调用
- 在此方法中可以得到用户的当前位置的mapView的中心点和经纬度跨度
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { MKCoordinateRegion region = mapView.region; // 经纬度跨度 MKCoordinateSpan span = region.span; NSLog(@"latitudeDelta = %f", span.latitudeDelta); NSLog(@"longitudeDelta = %f", span.longitudeDelta); }
②常见的代理方法有
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation; //一个位置更改默认只会调用一次,不断监测用户的当前位置 每次调用,都会把用户的最新位置(userLocation参数)传进来 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated; //地图的显示区域即将发生改变的时候调用 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; //地图的区域改变完成时调用
设置地图显示区域,和经纬度跨度
①通过MKMapView的下列方法,可以设置地图显示的位置和区域
// 设置地图的中心点位置 @property (nonatomic) CLLocationCoordinate2D centerCoordinate; - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated; // 设置地图的显示区域 @property (nonatomic) MKCoordinateRegion region; - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
②MKCoordinateRegion
//MKCoordinateRegion是一个用来表示区域的结构体,定义如下 typedef struct { CLLocationCoordinate2D center; // 区域的中心点位置 MKCoordinateSpan span; // 区域的跨度 } MKCoordinateRegion; //MKCoordinateSpan的定义 typedef struct { CLLocationDegrees latitudeDelta; // 纬度跨度 CLLocationDegrees longitudeDelta; // 经度跨度 } MKCoordinateSpan;
例:回到当前位置
// 1.拿到用户的经纬度 CLLocationCoordinate2D userCoordinate = self.mapView.userLocation.coordinate; // 2.设置地图的中心点坐标是用户的经纬度 // 2.1第一种设置方法 // [self.mapView setCenterCoordinate:userCoordinate animated:YES]; // 2.2第二种设置方法 /* span 可以在地图的区域改变完成时调用 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; */ MKCoordinateSpan span = {1.0, 1.0}; MKCoordinateRegion region = {userCoordinate, span}; [self.mapView setRegion:region animated:YES];
5.大头针
钉在某个具体位置,用来标识这个位置上有特定的事物(比如这个位置上有家餐馆)①大头针的基本操作
// 添加一个大头针 - (void)addAnnotation:(id <MKAnnotation>)annotation; // 添加多个大头针 - (void)addAnnotations:(NSArray *)annotations; // 移除一个大头针 - (void)removeAnnotation:(id <MKAnnotation>)annotation; // 移除多个大头针 - (void)removeAnnotations:(NSArray *)annotations;
(id )annotation参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据②大头针模型
遵守协议的任何模型对象为了改写协议中
属性--变量的值,重写这些变量
这里的
属性,只是为了定义get和set方法,不是所谓的传统属性
/** * 大头针的位置 */ @property (nonatomic, assign) CLLocationCoordinate2D coordinate; /** * 大头针标题 */ @property (nonatomic, copy) NSString *title; /** * 大头针的子标题 */ @property (nonatomic, copy) NSString *subtitle;
③添加大头针
创建大头针模型,给成员变量赋值
// 创建大头针模型 YLAnnotation *anno = [[YLAnnotation alloc] init]; anno.title = @"广东海洋大学"; anno.subtitle = @"广东省湛江市广东海洋大学"; CGFloat latitude = 24.00; CGFloat longitude = 103.76; anno.coordinate = CLLocationCoordinate2DMake(latitude , longitude);
添加大头针到地图上
[self.customMapView addAnnotation:anno];
6.自定义大头针
①每次添加大头针就会调用(地图上有几个大头针就调用几次)
/* * @param mapView 地图 * @param annotation 大头针模型 * * @return 大头针的view */ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;
注意: 如果返回nil, 系统会按照自己默认的方式显示
return nil;
创建大头针,给大头针附模型属性
类型 tableViewCellstatic NSString *identifier = @"anno"; // 1.从缓存池中取 MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; // 2.如果缓存池中没有, 创建一个新的 if (annoView == nil) { annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; // 设置大头针标题是否显示 // 自定义的大头针, 默认情况点击大头针之后是不会显示标题的, 需要我们自己手动设置显示 annoView.canShowCallout = YES; // 设置大头针标题显示的偏移位 annoView.calloutOffset = CGPointMake(-50, 0); // 设置大头针左边的辅助视图 annoView.leftCalloutAccessoryView = [[UISwitch alloc] init]; // 设置大头针右边的辅助视图 annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; } // 设置大头针的图片 annoView.image = [UIImage imageNamed:@"category_4"];
创建另一中大头针
MKAnnotationView的子类MKPinAnnotationView新增加了从天降效果,和
static NSString *identifier = @"anno"; // 1.从缓存池中取 MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; // 2.如果缓存池中没有, 创建一个新的 if (annoView == nil) { annoView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier]; // 设置大头针的颜色 annoView.pinColor = MKPinAnnotationColorPurple; // 设置大头针从天而降 annoView.animatesDrop = YES; // 设置大头针标题是否显示 // 自定义的大头针, 默认情况点击大头针之后是不会显示标题的, 需要我们自己手动设置显示 annoView.canShowCallout = YES; // 设置大头针标题显示的偏移位 annoView.calloutOffset = CGPointMake(-50, 0); // 设置大头针左边的辅助视图 annoView.leftCalloutAccessoryView = [[UISwitch alloc] init]; // 设置大头针右边的辅助视图 annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; } // 设置大头针的图片 // 注意: 如果你是使用的MKPinAnnotationView创建的自定义大头针, 那么设置图片无效, 因为系统内部会做一些操作, 覆盖掉我们自己的设置 annoView.image = [UIImage imageNamed:@"category_4"]; // 4.返回大头针View return annoView;
自定义AnnotationView
类型自定义Cell继承MKAnnotationView
// 每次添加大头针就会调用此方法 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { // NSLog(@"%s", __func__); NSLog(@"annotation === %@", annotation); // 对用户当前的位置的大头针特殊处理 if ([annotation isKindOfClass:[HMAnnotation class]] == NO) { return nil; } // 1.创建自定义大头针 YLAnnotationView *annoView = [YLAnnotationView annotationViewWithMap:mapView]; // 2.设置模型(可以不赋值,系统也会自动执行这个方法) // annoView.annotation = annotation; // 3.返回大头针 return annoView; }
自定义的AnnotationView中得方法
// 初始化方法 + (instancetype)annotationViewWithMap:(MKMapView *)mapView { static NSString *identifier = @"anno"; // 1.从缓存池中取 YLAnnotationView *annoView = (YLAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; // 2.如果缓存池中没有, 创建一个新的 if (annoView == nil) { // 参数传 nil 是因为 setAnnotation: 方法会赋值给Annotation模型 annoView = [[YLAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier]; } return annoView; } //- (void)setAnnotation:(id<MKAnnotation>)annotation // 此方法是重写set方法 - (void)setAnnotation:(YLAnnotation *)annotation { [super setAnnotation:annotation]; // 处理自己特有的操作 self.image = [UIImage imageNamed:annotation.icon]; }
相关文章推荐
- (转载)iOS开发中断言的使用—NSAssert()
- ios开发中的小技巧
- IOS使用navigationController跳转到下一页面黑屏问题。
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- IOS开发初学--XCODE篇
- iOS多线程编程之NSThread的使用
- IOS之pageControl
- iOS设计模式-模板方法
- iOS静态库与Workspace在项目中的使用
- iOS咱们一起学吧
- IOS NavigationBar 背景色
- iOS:KVO的概述与使用
- iOS定位--CoreLocation框架
- iOS 中数据持久化方案
- IOS开发入门之工程创建
- IOS 整体框架类图值得收藏
- iOS 新浪微博-1.0框架搭建
- collectionView学习
- iOS 多线程技术2
- IOS 友盟错误分析