1.MapKit框架使用前提

①导入框架

②导入主头文件

#import <MapKit/MapKit.h>

③MapKit框架使用须知

  • MapKit框架中所有数据类型的前缀都是MK
  • MapKit有一个比较重要的UI控件,专门用于地图显示

④MapKit框架包含CLLocation

2.设置地图的类型

可以通过设置MKMapView的mapType设置地图类型

typedef enum : NSUInteger {
MKMapTypeStandard , 标准(默认)
MKMapTypeSatellite ,卫星
MKMapTypeHybrid 混合(标准 + 卫星)
} MKMapType;

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;

创建大头针,给大头针附模型属性

  • 类型 tableViewCell
static 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]; }

最新文章

  1. Spring+quartz 实现定时任务job集群配置
  2. Android线程处理之Handler
  3. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
  4. WEB的相关知识总结
  5. 安装Nuget上常用的包的命令
  6. 对ContentProvider中getType方法的一点理解
  7. iOS-实现映客首页TabBar和滑动隐藏NavBar和TabBar
  8. IT English Collection(9) of Objective-C
  9. rovio 视觉里程计的笔记
  10. 新手OTCBTC注册前一定要看的几个步骤!【安全性相关】
  11. Oracle:FOR循环语句练习
  12. Android MVP
  13. Panel 中加载窗体
  14. github删除
  15. skywalking部署
  16. jQuery的介绍
  17. 【JavaScript】--- ES6/ES7/ES8
  18. 微信小程序双击事件的绑定
  19. MAC软件下载比较好的三个第三方网站
  20. [翻译]Spring MVC RESTFul Web Service CRUD 例子

热门文章

  1. [学习笔记]overthewire bandit 通关秘籍
  2. 一个DIV相对于另一个DIV定位
  3. C项目实践--学生成绩管理系统
  4. Flame Graphs
  5. (转)Java中JSON字符串与java对象的互换实例详解
  6. go---weichart个人对Golang中并发理解
  7. YTU 2435: C++ 习题 输出日期时间--友元函数
  8. hash与map的区别联系应用
  9. CodeForces-213E:Two Permutations(神奇的线段树+hash)
  10. BZOJ_3620_似乎在梦中见过的样子_KMP