一、MapKit框架的使用

MapKit框架使用须知
MapKit框架中所有数据类型的前缀都是MK
MapKit有一个比较重要的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;
地图的显示区域已经发生改变的时候调用
 
四、MKUserLocation
MKUserLocation其实是个大头针模型,包括以下属性
@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;
 
六、MKCoordinateRegion
MKCoordinateRegion是一个用来表示区域的结构体,定义如下

typedef struct {

CLLocationCoordinate2D center; // 区域的中心点位置

MKCoordinateSpan span; // 区域的跨度

} MKCoordinateRegion;

MKCoordinateSpan的定义

typedef struct {

CLLocationDegrees latitudeDelta; // 纬度跨度

CLLocationDegrees longitudeDelta; // 经度跨度

} MKCoordinateSpan;

七、大头针

1.大头针的基本操作

添加一个大头针
- (void)addAnnotation:(id <MKAnnotation>)annotation;
 
添加多个大头针
- (void)addAnnotations:(NSArray *)annotations;
 
移除一个大头针
- (void)removeAnnotation:(id <MKAnnotation>)annotation;
 
移除多个大头针
- (void)removeAnnotations:(NSArray *)annotations;
 
(id <MKAnnotation>)annotation参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据

2.大头针模型

新建一个大头针模型类

#import <MapKit/MapKit.h>

@interface MJTuangouAnnotation : NSObject <MKAnnotation>

/** 坐标位置 */

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

/** 标题 */

@property (nonatomic, copy) NSString *title;

/** 子标题 */

@property (nonatomic, copy) NSString *subtitle;

@end

 
3.添加大头针

MJTuangouAnnotation *anno = [[MJTuangouAnnotation alloc] init];

anno.title = @"传智播客iOS学院";

anno.subtitle = @"全部课程15折,会员20折,老学员30折";

anno.coordinate = CLLocationCoordinate2DMake(40, 116);

[self.mapView addAnnotation:anno];

八、自定义大头针

1.如何自定义大头针
设置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

地图上的大头针控件是MKAnnotationView
 
MKAnnotationView的属性
@property (nonatomic, strong) id <MKAnnotation> annotation;
大头针模型
 
@property (nonatomic, strong) UIImage *image;
显示的图片
 
@property (nonatomic) BOOL canShowCallout;
是否显示标注
 
@property (nonatomic) CGPoint calloutOffset;
标注的偏移量
 
@property (strong, nonatomic) UIView *rightCalloutAccessoryView;
标注右边显示什么控件
@property (strong, nonatomic) UIView *leftCalloutAccessoryView;
标注左边显示什么控件
 
MKPinAnnotationView是MKAnnotationView的子类
 
MKPinAnnotationView比MKAnnotationView多了2个属性
@property (nonatomic) MKPinAnnotationColor pinColor;
大头针颜色
 
@property (nonatomic) BOOL animatesDrop;
大头针第一次显示时是否从天而降

代码

 //
// ViewController.m
// IOS_0403_MapKit基本使用
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController ()<MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *customMapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) CLGeocoder *geocoder; @end @implementation ViewController /*
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述 */ - (void)viewDidLoad {
[super viewDidLoad];
[self test];
} - (void)test
{
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
//请求权限
self.mgr = [[CLLocationManager alloc] init];
[self.mgr requestAlwaysAuthorization];
}
self.customMapView.delegate = self; //设置地图类型
self.customMapView.mapType = MKMapTypeStandard;
//设置不允许旋转
self.customMapView.rotateEnabled = NO;
//追踪我的位置
self.customMapView.userTrackingMode = MKUserTrackingModeFollow;
}
#pragma mark - MKMapViewDelegate //每次更新用户的位置就会调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ //反向地理编码
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks firstObject];
//设置大头针显示内容
userLocation.title = placeMark.name;
userLocation.subtitle = placeMark.locality;
}]; //移动地图到当前用户所在的位置
[self.customMapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; //设置地图显示的区域
//获取用户位置
CLLocationCoordinate2D center = userLocation.location.coordinate;
//设置经纬度跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.customMapView setRegion:region animated:YES]; }
//地图区域将要改变时调用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
NSLog(@"regionWillChangeAnimated");
} //地图区域改变完成时调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"regionDidChangeAnimated"); NSLog(@"%f %f", self.customMapView.region.span.latitudeDelta, self.customMapView.region.span.longitudeDelta);
} #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if(!_geocoder){
_geocoder = [[CLGeocoder alloc] init]; }
return _geocoder;
} @end

自定义大头针

 //
// ViewController.m
// IOS_0403_MapKit基本使用
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController ()<MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *customMapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@property (nonatomic, strong) CLGeocoder *geocoder; @end @implementation ViewController /*
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述 */ - (void)viewDidLoad {
[super viewDidLoad];
[self test];
} - (void)test
{
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
//请求权限
self.mgr = [[CLLocationManager alloc] init];
[self.mgr requestAlwaysAuthorization];
}
self.customMapView.delegate = self; //设置地图类型
self.customMapView.mapType = MKMapTypeStandard;
//设置不允许旋转
self.customMapView.rotateEnabled = NO;
//追踪我的位置
self.customMapView.userTrackingMode = MKUserTrackingModeFollow;
}
#pragma mark - MKMapViewDelegate //每次更新用户的位置就会调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ //反向地理编码
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks firstObject];
//设置大头针显示内容
userLocation.title = placeMark.name;
userLocation.subtitle = placeMark.locality;
}]; //移动地图到当前用户所在的位置
[self.customMapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; //设置地图显示的区域
//获取用户位置
CLLocationCoordinate2D center = userLocation.location.coordinate;
//设置经纬度跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.customMapView setRegion:region animated:YES]; }
//地图区域将要改变时调用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
NSLog(@"regionWillChangeAnimated");
} //地图区域改变完成时调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"regionDidChangeAnimated"); NSLog(@"%f %f", self.customMapView.region.span.latitudeDelta, self.customMapView.region.span.longitudeDelta);
} #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if(!_geocoder){
_geocoder = [[CLGeocoder alloc] init]; }
return _geocoder;
} @end //
// Annotation.m
// IOS_0403_自定义大头针
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "Annotation.h" @implementation Annotation @end
 //
// BZAnnotationView.h
// IOS_0403_自定义大头针
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import <MapKit/MapKit.h> @interface BZAnnotationView : MKAnnotationView
/**
* 快速创建方法
*
* @param mapView 地图
*
* @return 大头针
*/
+ (instancetype)annotationViewWithMap:(MKMapView *)mapView; @end //
// BZAnnotationView.m
// IOS_0403_自定义大头针
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "BZAnnotationView.h"
#import "Annotation.h" @implementation BZAnnotationView - (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
// 初始化
// 设置大头针标题是否显示
self.canShowCallout = YES; // 设置大头针左边的辅助视图
self.leftCalloutAccessoryView = [[UISwitch alloc] init]; // 设置大头针右边的辅助视图
self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}
return self;
} + (instancetype)annotationViewWithMap:(MKMapView *)mapView
{
static NSString *identifier = @"anno"; // 1.从缓存池中取
BZAnnotationView *annoView = (BZAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// 2.如果缓存池中没有, 创建一个新的
if (annoView == nil) {
annoView = [[BZAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
}
return annoView;
} //- (void)setAnnotation:(id<MKAnnotation>)annotation
- (void)setAnnotation:(Annotation *)annotation
{
[super setAnnotation:annotation]; //处理自己特有的操作
self.image = [UIImage imageNamed:annotation.icon]; } @end
 //
// ViewController.m
// IOS_0403_自定义大头针
//
// Created by ma c on 16/4/3.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import <MapKit/MapKit.h>
#import "Annotation.h"
#import "BZAnnotationView.h" @interface ViewController ()<MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *customMapView;
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic, strong) CLLocationManager *mgr;
- (IBAction)addAnno; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self test];
} - (void)test
{
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
//请求权限
self.mgr = [[CLLocationManager alloc] init];
[self.mgr requestAlwaysAuthorization];
}
self.customMapView.delegate = self; //设置地图类型
self.customMapView.mapType = MKMapTypeStandard;
//设置不允许旋转
self.customMapView.rotateEnabled = NO;
//追踪我的位置
self.customMapView.userTrackingMode = MKUserTrackingModeFollow;
} //添加大头针
- (IBAction)addAnno {
// 创建大头针模型
Annotation *anno = [[Annotation alloc] init];
anno.title = @"传智";
anno.subtitle = @"育新小区";
CGFloat latitude = 36.821199 + arc4random_uniform();
CGFloat longitude = 116.858776 + arc4random_uniform();
anno.coordinate = CLLocationCoordinate2DMake(latitude , longitude);
anno.icon = @"category_1"; // 添加大头针
[self.customMapView addAnnotation:anno];
} #pragma mark - MKMapViewDelegate //每次更新用户的位置就会调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ //反向地理编码
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks firstObject];
//设置大头针显示内容
userLocation.title = placeMark.name;
userLocation.subtitle = placeMark.locality;
}]; //移动地图到当前用户所在的位置
[self.customMapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
} - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
//对用户当前位置的大头针特殊处理
if ([annotation isKindOfClass:[Annotation class]] == NO) {
//注意: 如果返回nil, 系统会按照自己默认的方式显示
return nil;
} // 1.创建大头针
BZAnnotationView *annoView = [BZAnnotationView annotationViewWithMap:mapView];
// 2.设置模型
annoView.annotation = annotation; /*
//默认情况下MKAnnotationView是无法显示的,如果想自定义大头针可以使用其子类MKPinAnnotationView
//自定义大头针默认情况下,点击不会显示标题,需要我们手动显示 static NSString *identifier = @"annotation";
// 1.从缓存池中取
MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// 2.缓存池中没有创建
if (!annoView) {
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier]; //设置大头针颜色
annoView.pinTintColor = [UIColor orangeColor];
//设置大头针从天而降
annoView.animatesDrop = YES;
//设置大头针标题显示
annoView.canShowCallout = YES;
//设置大头针标题显示的偏移
annoView.calloutOffset = CGPointMake(0, 30);
//设置大头针辅助视图
annoView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//设置大头针图片
//注意: 如果你是使用的MKPinAnnotationView创建的自定义大头针, 那么设置图片无效, 因为系统内部会做一些操作, 覆盖掉我们自己的设置
//annoView.image = [UIImage imageNamed:@"category_4"]; }
// 3.给大头针View设置数据
annoView.annotation = annotation; */ return annoView;
} #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if(!_geocoder){
_geocoder = [[CLGeocoder alloc] init]; }
return _geocoder;
} @end

IOS-MapKit的更多相关文章

  1. iOS Mapkit 定位REGcode地理位置偏移

    在iOS上,使用系统Mapkit定位,获取到的坐标会有偏移: 今有需求,用系统Mapkit定位,并Regcode出实际地理位置,修正偏移: 解决方案: 使用MapView的代理 - (void)map ...

  2. iOS MapKit地图

    地图框架:#import <MapKit/MapKit.h> 基本属性和方法: 属性: 地图类视图:MKMapView 地图类型:MKMapType mapType 地图旋转:rotate ...

  3. iOS MapKit导航及地理转码辅助类

    头文件: #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface DirectionRou ...

  4. IOS MapKit框架的使用(专门用于地图显示)

    ● MapKit框架使用前提 ● 导入框架 ● 导入主头文件#import <MapKit/MapKit.h>   ●  MapKit框架使用须知 ●  MapKit框架中所有数据类型的前 ...

  5. iOS项目开发中的知识点与问题收集整理②(Part 二)

    1.点击UIButton 无法产生触摸事件    如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...

  6. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  7. iOS开发---集成百度地图

    由于iOS MapKit框架很多情况并不能满足我们的需求,我们可以选择集成百度地图,那该如何操作呢? 申请Key 登录百度API管理中心申请Key http://lbsyun.baidu.com/ap ...

  8. iOS 开发笔记

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 2,NSDate使用 3,UTTabviewCell 未 ...

  9. iOS项目开发知识点

    前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...

  10. iOS项目开发中的知识点与问题收集整理②

    1.点击UIButton 无法产生触摸事件    如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...

随机推荐

  1. 【pip】【conda】

    1.指定python包安裝版本==  指定python包安裝源-i: pip2 -i https://pypi.tuna.tsinghua.edu.cn/simple 2.配置文件换源 vi ~/.p ...

  2. x265探索与研究(六):main()函数

    x265探索与研究(六):main()函数 x265源代码的入口函数是main(),本文分析main()的主要功能. 首先给出main()函数的功能及其代码结构:其次给出main()函数源代码以及分析 ...

  3. 在Docker上安装配置Oracle教程

    地址:https://github.com/wnameless/docker-oracle-xe-11g Docker shell 下: docker pull wnameless/oracle-xe ...

  4. 009-JDK可视化监控工具-JConsole

    Console工具在JDK/bin目录下,启动JConsole后,将自动搜索本机运行的jvm进程,不需要jps命令来查询指定.双击其中一个jvm进程即可开始监控,也可使用“远程进程”来连接远程服务器. ...

  5. Android学习十二---在android上实现图像匹配

    一.效果图及功能描述 效果图 点击ShowImg后 点击match,然后点击showmatch,可以不断点击showmatch. 主要功能描述:显示在SD卡上已经存在的图片test.jpg,根据图片在 ...

  6. 开博第一篇,学习markdown

    Markdown学习 其实之前自己也一直有记录,不过是Evernote记录,没有分享出来,最近看了一些牛人博客,觉得也应该分享出来.和别人多交流,多学习.所以花了几小时学了一下Markdown语法,现 ...

  7. eclipse 安装 spring boot suite 插件遇到的问题

    问题:安装失败,报如下错误: An error occurred while collecting items to be installedsession context was:(profile= ...

  8. html-3,table 表格标签 tr th td caption thead tbody tfoot 的简单使用

    <!-- table border='1' style="border-collapse:collapse;" border 表格的像素宽度 border-collapse: ...

  9. ocx的容器调试和ie调试

    1 容器调试 1.1 F5,选择ActivesX Control Test Containor 1.2 ok--ok,选择工具栏的new control 1.3 在Insert Control内选择你 ...

  10. 【Thinking in java, 4e】复用类

    mark一篇09年的<Thinking in Java>笔记:here --> https://lawrence-zxc.github.io/2009/11/07/thinking- ...