IOS-MapKit
一、MapKit框架的使用
typedef struct {
CLLocationCoordinate2D center; // 区域的中心点位置
MKCoordinateSpan span; // 区域的跨度
} MKCoordinateRegion;
typedef struct {
CLLocationDegrees latitudeDelta; // 纬度跨度
CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;
七、大头针
1.大头针的基本操作
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
MJTuangouAnnotation *anno = [[MJTuangouAnnotation alloc] init];
anno.title = @"传智播客iOS学院";
anno.subtitle = @"全部课程15折,会员20折,老学员30折";
anno.coordinate = CLLocationCoordinate2DMake(40, 116);
[self.mapView addAnnotation:anno];
八、自定义大头针
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(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
代码
//
// 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的更多相关文章
- iOS Mapkit 定位REGcode地理位置偏移
在iOS上,使用系统Mapkit定位,获取到的坐标会有偏移: 今有需求,用系统Mapkit定位,并Regcode出实际地理位置,修正偏移: 解决方案: 使用MapView的代理 - (void)map ...
- iOS MapKit地图
地图框架:#import <MapKit/MapKit.h> 基本属性和方法: 属性: 地图类视图:MKMapView 地图类型:MKMapType mapType 地图旋转:rotate ...
- iOS MapKit导航及地理转码辅助类
头文件: #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface DirectionRou ...
- IOS MapKit框架的使用(专门用于地图显示)
● MapKit框架使用前提 ● 导入框架 ● 导入主头文件#import <MapKit/MapKit.h> ● MapKit框架使用须知 ● MapKit框架中所有数据类型的前 ...
- iOS项目开发中的知识点与问题收集整理②(Part 二)
1.点击UIButton 无法产生触摸事件 如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发---集成百度地图
由于iOS MapKit框架很多情况并不能满足我们的需求,我们可以选择集成百度地图,那该如何操作呢? 申请Key 登录百度API管理中心申请Key http://lbsyun.baidu.com/ap ...
- iOS 开发笔记
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 2,NSDate使用 3,UTTabviewCell 未 ...
- iOS项目开发知识点
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- iOS项目开发中的知识点与问题收集整理②
1.点击UIButton 无法产生触摸事件 如果在UIImageView中添加了一个按钮,你会发现在默认情况下这个按钮是无法被点击的,需要设置UIImageView的userInteractio ...
随机推荐
- mysql乐观锁总结和实践(转)
原文:mysql乐观锁总结和实践 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任何场景,它也有它存在的一些不足,因为悲观锁大多数情况下依靠数据库的 ...
- 配置 Docker 镜像下载的本地 mirror 服务
Docker registry 工具如今已经非常好的支持了 mirror 功能,使用它能够配置一个本地的 mirror 服务.将 pull 过的镜像 cache 在本地.这样其他主机再次 pull 的 ...
- Linux运维工程师:30道面试题整理
1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...
- Android图片加载框架之Picasso
相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...
- 安装指定版本的Ionic或Cordova(转载)
安装ionic 及 cordova npm install -g cordova ionic 更新命令 npm update -g cordova ionic 安装特定版本 npm install - ...
- Python3.6写socket程序
Python进行Socket程序编写使用的主要模块就是 socket 模块,在这个模块中可以找到 socket()函数,该函数用于创建套接字对象.套接字也有自己的方法集,这些方法可以实现基于套接字的网 ...
- mysql多实例的配置
1.创建多实例的目录: mkdir -p /data/mysql/mysql_3307/{data,tmp,logs} mkdir -p /data/mysql/mysql_3308/{data,tm ...
- Hadoop2.0中单点故障解决方案总结---老董
Hadoop 1.0内核主要由两个分支组成:MapReduce和HDFS,众所周知,这两个系统的设计缺陷是单点故障,即MR的JobTracker和HDFS的NameNode两个核心服务均存在单点问题, ...
- php优化,操作码优化,缓存优化
一.php缓存加速器软件种类 xcache,eaccelerator,zend,apc如何选择:建议xcache,eaccelerator,二选一,首选xcachexcache更快 二.php缓存加速 ...
- python 对象和类
python中所有数据都是以对象形式存在.对象既包含数据(变量),也包含代码(函数),是某一类具体事物的特殊实例. 面向对象的三大特性为封装.继承和多态. 1.定义类 #定义空类 class Pers ...