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 ...
随机推荐
- 转!!SQL左右连接中的on and和on where的区别
原博文地址:http://blog.csdn.net/xingzhemoluo/article/details/39677891 原先一直对SQL左右连接中的on and和on where的区别不是太 ...
- element-UI中el-select下拉框可搜索时候,filter-method自定义搜索方法
使用element-UI框架的使用,我们经常使用el-select下拉框,很多时候还需要使用可搜索的下拉框,然后elementUI官网的实例中只是提了一下filter-method可以自定义搜索方法, ...
- 11.Query an Array of Embedded Documents-官方文档摘录
总结 1.插入数据 db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A&qu ...
- window下安装mysql详细步骤
1.下载安装包 打开mysql官网下载页面:http://dev.mysql.com/downloads/mysql/ 1.选择相应的版本和平台 2.mysql配置 打开刚刚解压的文件夹F:\mysq ...
- 在python列表中删除所有空元素
今天在测试数据的时候偶然发现一个问题,如下: test = ['a','','b','','c','',''] for i in test: if i == '': test.remove(i) pr ...
- Mahout学习路线图-张丹老师
前言 Mahout是Hadoop家族中与众不同的一个成员,是基于一个Hadoop的机器学习和数据挖掘的分布式计算框架.Mahout是一个跨学科产品,同时也是我认为Hadoop家族中,最有竞争力,最难掌 ...
- JAVA寄存器
所有进程都使用寄存器,Java虚拟机使用下列寄存器管理系统堆栈: 程序记数寄存器:跟踪程序执行的准确位置 堆栈指针寄存器:指示操作栈项 框架寄存器:指向当前执行的环境 变量寄存 ...
- ZOJ - 3593 One Person Game (扩展欧几里得)
题意:一个人在坐标A,要前往坐标B的位置.可以往左或往右走a,b,a+b个单位,求到达B的最小步数. 分析:扩展欧几里得算法求解线性方程的套路不变.令C=fabs(A-B),c = a+b, 扩展gc ...
- jmeter 分布式集群
Jmeter压测过程中,由于测试机配置有限,CPU.内存都可能是存在瓶颈.如果使用很大的并发进行测试时,就可能会感到程序比较卡,这时候就无法继续增加压力了. 解决方法: 搭建Jmeter分布式集群,远 ...
- C# Winform 窗体传值 利用委托 子窗体传值给父窗体
常用的Winform窗体传值有两种方式. 1.更改Form.designer.cs文件,将控件的设为Public,供子窗体访问. 在designer.cs文件的最后,找到你的控件声明. private ...