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 ...
随机推荐
- time 模块,random模块,os模块
一 :time 模块 python中,通常有几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...
- Linux运维工程师:30道面试题整理
1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...
- 0407-服务注册与发现-Eureka深入理解-元数据、高可用HA
一.Eureka元数据 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_e ...
- Kotlin 初级读本
第一部分——快速上手第一章·启程 第二章·基本语法第三章·Kotlin 与 Java 混编 第二部分——开始学习 Kotlin第四章·Kotlin 的类特性(上)第四章·Kotlin 的类特性(下)第 ...
- ajax请求真实服务器数据示例
// http://study.163.com/webDev/couresByCategory.htm // http://study.163.com/webDev/couresByCategory. ...
- if 条件判断
逻辑判断的布尔值(true&false) 1.逻辑值(bool)用来表示诸如:对与错,真与假,非于空等概念. 2.逻辑值包含了两个值:--true:表示非空的量(比如:string,tuple ...
- HTML5之概述
HTML5是万维网的核心语言.标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改,是继HTML4.01和XHTML1.0之后的超文本标记语言的最新版本.它是由一群自由思想者组成的团 ...
- CodeForces - 786B Legacy (线段树+DIjkstra+思维)
题意:给N个点和Q条选项,有三种类型的选项:1.从u到v花费w修建一条路:2.从u到下标区间为[L,R]的点花费w修建一条路; 3.从下标区间为[L,R]的点到u花费w修建一条路. 然后求起点s到其余 ...
- HDU - 1151 Air Raid (最小路径覆盖)
题意:给定一个有向无环图,求最少划分几条路径,使之能够覆盖所有点. 分析:这可以转化为DAG上的最小路径覆盖问题. 路径覆盖的定义:有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且 ...
- java @Retention元注解
@Retention元注解 有三种取值:RetentionPolicy.SOURCE.RetentionPolicy.CLASS.RetentionPolicy.RUNTIME分别对应:Java源文件 ...