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原来还能这么导出数据,涨知识了. 方式1: select ....(sql语句) INTO OUTFILE '/var/lib/mysql/msg_data.csv ' (导出的文件位置 ...
- phpexcel导出带生成图片完美案例
// 导出exl public function look_down(){ $id = I('get.id'); $m = M ('offer_goods'); $where['offer_id'] ...
- python16_day09【Select多路复用】
一.select多路复用 句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间) 参数: 可接受四个参数(前三个必须) 返回 ...
- 一个很牛的计算pi的c程序!
C语言是面向过程的一种高级程序设计语言,它在世界范围内使用很广泛,而且很流行.很多大型的应用软件,基本上是用C语言所编写的.在对操作系统以及系统使用程序.需要对硬件进行操作的场合,C语言较其他的高级语 ...
- 搭建自己的npm仓库
第一步:安装Erlang环境 首先,安装必要的库 yum install build-essential yum install libncurses5-dev yum install libssl- ...
- 跟着实例学习ZooKeeper的用法: 分布式锁
锁 分布式的锁全局同步, 这意味着任何一个时间点不会有两个客户端都拥有相同的锁. 可重入锁Shared Reentrant Lock 首先我们先看一个全局可重入的锁. Shared意味着锁是全局可见的 ...
- java GC 回收机制 转
JVM分代垃圾回收策略的基础概念 由于不同对象的生命周期不一样,因此在JVM的垃圾回收策略中有分代这一策略.本文介绍了分代策略的目标,如何分代,以及垃圾回收的触发因素. 文章总结了JVM垃圾回收策略为 ...
- MongoDB win32-bit 安装
一念起: 由于本人 用的电脑比较老旧,所以一直用的 是win7 32bit 的操作系统,但是在学习MongoDB的时候 遇到了起步的第一个问题,按照目前 官网最新版MongoDB 3.4.3,已不支持 ...
- 使用Socket&反射&Java流操作进行方法的远程调用(模拟RPC远程调用)
写在前面 阅读本文首先得具备基本的Socket.反射.Java流操作的基本API使用知识:否则本文你可能看不懂... 服务端的端口监听 进行远程调用,那就必须得有客户端和服务端.服务端负责提供服务,客 ...
- shiro的过滤器
shiro的过滤器也是不多的我们可以自定义的方法,它的继承体系如下: 另外UserFilter是继承于AccessControlFilter 1.NameableFilter NameableFilt ...