使用MapKit框架(持续更新)
使用MapKit框架
地图显示
最简单显示地图的代码:
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化地图控件
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; // 地图的类型
mapView.mapType = MKMapTypeStandard; // 视图的宽度和高度将和父视图的宽度一起成比例变化
mapView.autoresizingMask = \
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // 显示地图
[self.view addSubview:mapView];
} @end
RootViewController
注意:使用地图之前是需要引入MapKit框架的哦.
autoresizingMask是干什么用的呢(实际上,我看过后还是不懂)?
UIViewAutoresizingNone |
这个常量如果被设置,视图将不进行自动尺寸调整。 |
UIViewAutoresizingFlexibleHeight |
这个常量如果被设置,视图的高度将和父视图的高度一起成比例变化。否则,视图的高度将保持不变。 |
UIViewAutoresizingFlexibleWidth |
这个常量如果被设置,视图的宽度将和父视图的宽度一起成比例变化。否则,视图的宽度将保持不变。 |
UIViewAutoresizingFlexibleLeftMargin |
这个常量如果被设置,视图的左边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的左边界的相对位置将保持不变。 |
UIViewAutoresizingFlexibleRightMargin |
这个常量如果被设置,视图的右边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的右边界的相对位置将保持不变。 |
UIViewAutoresizingFlexibleBottomMargin |
这个常量如果被设置,视图的底边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的底边界的相对位置将保持不变。 |
UIViewAutoresizingFlexibleTopMargin |
这个常量如果被设置,视图的上边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的上边界的相对位置将保持不变。 |
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化地图控件
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; // 地图的类型
mapView.mapType = MKMapTypeStandard; // 视图的宽度和高度将和父视图的宽度一起成比例变化
mapView.autoresizingMask = \
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // 显示用户所在位置(此处系统会询问你是否使用当前位置)
mapView.showsUserLocation = YES; // 经纬度坐标(河南南阳)
CLLocationCoordinate2D coor2d = {33.00, 112.52}; // 地图缩放级别
MKCoordinateSpan span = {, }; // 被显示的区域
MKCoordinateRegion region = {coor2d, span}; // 设置显示的区域
[mapView setRegion:region]; // 显示地图
[self.view addSubview:mapView]; } @end
RootViewController.m
位置定位
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 判断定位功能是否可以使用
if([CLLocationManager locationServicesEnabled])
{
// 初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self; // 开始定位
[_locationManager startUpdatingLocation];
}
else
{
NSLog(@"定位功能不可用");
}
} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSLog(@"%@", newLocation); // 定位结束
[manager stopUpdatingLocation];
} - (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%@", error); // 定位结束
[manager stopUpdatingLocation];
} @end
RootViewController.m
为什么需要判断定位功能可不可用呢?下图可以看出为什么了.
打印信息:
2014-05-15 09:25:11.883 CoreLocation[17610:60b] <+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 5/15/14, 9:25:11 AM China Standard Time
本人将这个代理定位的方式改写成了可以使用block的方式:
YXLocation.h
//
// YXLocation.h
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h>
#import <MapKit/MapKit.h> @protocol YXLocationProtocol <NSObject> @optional
- (void)currentLocation:(CLLocation *)location sucess:(BOOL)sucess; @end typedef void (^locationBlock_t)(CLLocation *currentLocation, BOOL sucess); @interface YXLocation : NSObject @property (nonatomic, copy, readwrite) locationBlock_t locationBlock;
@property (nonatomic, assign, readwrite) id<YXLocationProtocol> protocol; @property (nonatomic, strong, readonly) CLLocation *location; - (void)start; @end
Code
YXLocation.m
//
// YXLocation.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "YXLocation.h" @interface YXLocation ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @end @implementation YXLocation - (instancetype)init
{
self = [super init];
if (self)
{
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return self;
} - (void)start
{
if([CLLocationManager locationServicesEnabled])
{
[_locationManager startUpdatingLocation];
}
else
{
NSLog(@"定位功能没有开启"); if (_locationBlock)
{
_locationBlock(nil, NO);
} if (_protocol)
{
[_protocol currentLocation:nil sucess:NO];
}
}
} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject]; _location = newLocation; if (_locationBlock)
{
_locationBlock(newLocation, YES);
} if (_protocol)
{
[_protocol currentLocation:newLocation sucess:YES];
} [_locationManager stopUpdatingLocation];
} - (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"定位功能出错"); if (_locationBlock)
{
_locationBlock(nil, NO);
} if (_protocol)
{
[_protocol currentLocation:nil sucess:NO];
}
} @end
Code
将经纬度转换为有意义的地址
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geocoder; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 判断定位功能是否可以使用
if([CLLocationManager locationServicesEnabled])
{
// 初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self; // 开始定位
[_locationManager startUpdatingLocation];
}
else
{
NSLog(@"定位功能不可用");
}
} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject]; // 初始化经纬度解析器
_geocoder = [[CLGeocoder alloc] init]; // 解析经纬度值
[_geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:]; // 将字典信息拼接起来
NSString *locatedAt = \
[[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; // 打印信息
NSLog(@"我在 %@",locatedAt); NSLog(@"国家代码 %@",placemark.ISOcountryCode);
NSLog(@"国家 %@",placemark.country);
NSLog(@"邮政编码 %@",placemark.postalCode);
NSLog(@"administrativeArea %@",placemark.administrativeArea);
NSLog(@"locality %@",placemark.locality);
NSLog(@"subLocality %@",placemark.subLocality);
NSLog(@"subThoroughfare %@",placemark.subThoroughfare);
}]; // 定位结束
[manager stopUpdatingLocation];
} - (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%@", error); // 定位结束
[manager stopUpdatingLocation];
} @end
RootViewController.m
打印信息如下:
2014-05-15 09:40:13.982 CoreLocation[2482:60b] 我在 中国北京市东城区东四街道东四朝阳门北小街2-1号
2014-05-15 09:40:13.986 CoreLocation[2482:60b] 国家代码 CN
2014-05-15 09:40:13.987 CoreLocation[2482:60b] 国家 中国
2014-05-15 09:40:13.988 CoreLocation[2482:60b] 邮政编码 (null)
2014-05-15 09:40:13.989 CoreLocation[2482:60b] administrativeArea 北京市
2014-05-15 09:40:13.991 CoreLocation[2482:60b] locality (null)
2014-05-15 09:40:13.992 CoreLocation[2482:60b] subLocality 东城区
2014-05-15 09:40:13.993 CoreLocation[2482:60b] subThoroughfare 2-1号
将有意义的地址转换为经纬度
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLGeocoder *geocoder; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; _geocoder = [[CLGeocoder alloc] init]; [_geocoder geocodeAddressString:@"中国北京市东城区东四街道东四朝阳门北小街2-1号"
completionHandler:^(NSArray *placemarks, NSError *error)
{ if([placemarks count] > && error == nil)
{ NSLog(@"发现了 %lu placemark(s).",(unsigned long)[placemarks count]); CLPlacemark *firstPlacemark = [placemarks objectAtIndex:]; NSLog(@"经度 = %f",firstPlacemark.location.coordinate.longitude);
NSLog(@"纬度 = %f",firstPlacemark.location.coordinate.latitude); }
else if ([placemarks count] == && error == nil)
{
NSLog(@"没有找到 placemarks."); }
else if (error != nil)
{
NSLog(@"错误 = %@",error);
}
}];
} @end
RootViewController.m
打印信息:
2014-05-15 09:51:15.270 CoreLocation[2525:60b] 发现了 2 placemark(s).
2014-05-15 09:51:15.274 CoreLocation[2525:60b] 经度 = 116.425960
2014-05-15 09:51:15.275 CoreLocation[2525:60b] 纬度 = 39.931609
直接显示用户当前位置
//
// RootViewController.m
// CoreLocation
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import <MapKit/MapKit.h> @interface RootViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 判断定位功能是否可以使用
if([CLLocationManager locationServicesEnabled])
{
// 初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self; // 开始定位
[_locationManager startUpdatingLocation];
}
else
{
NSLog(@"定位功能不可用");
}
} - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// 获取坐标信息
CLLocation *newLocation = [locations lastObject]; // 定位结束
[manager stopUpdatingLocation]; // 初始化地图控件
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; // 地图的类型
mapView.mapType = MKMapTypeStandard; // 视图的宽度和高度将和父视图的宽度一起成比例变化
mapView.autoresizingMask = \
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // 显示用户所在位置(此处系统会询问你是否使用当前位置)
mapView.showsUserLocation = YES; // 地图缩放级别
MKCoordinateSpan span = {0.02, 0.02}; // 被显示的区域
MKCoordinateRegion region = {newLocation.coordinate, span}; // 设置显示的区域
[mapView setRegion:region]; // 显示地图
[self.view addSubview:mapView];
} - (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%@", error); // 定位结束
[manager stopUpdatingLocation];
} @end
RootViewController.m
使用MapKit框架(持续更新)的更多相关文章
- iOS之github第三方框架(持续更新)
1.MBProgressHUD MBProgressHUD是一个开源项目,实现了很多种样式的提示框 使用上简单.方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到. 到Github ...
- 收集前端UI框架 持续更新中....
1. elementUI 饿了么出品 基于Vue2 http://element.eleme.io/#/zh-CN 2. ZUI 开源HTML5跨屏框架 (2018年1月4日更新)一个基于 Boot ...
- ECLIPSE/JAVAWEB (二)三大框架之Hibernate框架 持续更新中...
(一)发展历史 在Struts框架中使用jdbc连接来读写数据库,我们最常见的就是打开数据库连接.使用复杂的sql语句进行读写.关闭连接,获得的数据又需要转换或封装后往外传,这是一个非常繁琐的过程. ...
- Eclipse/JavaWeb (三)三大框架之Spring框架 持续更新中...
(一)发展历史 现在我们有三个层了,可是每层之间的调用是怎样的呢?比如显示层的struts需要调用一个业务类,就需要new一个业务类出来,然后使用:业务层需要调用持久层的类,也需要new一个持久层类出 ...
- Eclipse/JavaWeb (一)三大框架之struts框架 持续更新中...
最近主要把javaweb的三大框架过一遍. (一)发展历史 传统的Java Web应用程序是采用JSP+Servlet+Javabean来实现的,这种模式实现了最基本的MVC分层,使得程序分为几层,有 ...
- iOS开发常用第三方开源框架 持续更新中...
键盘管理 TPKeyboardAvoiding IQKeyboardManager(1.2.8) 弹窗HUD MBProgressHUD(0.9.2) SVProgressHUD UIView+Toa ...
- iOS开发系列文章(持续更新……)
iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...
- 我的敏捷、需求分析、UML、软件设计电子书 - 下载(持续更新中)
我将所有我的电子书汇总在一起,方便大家下载!(持续更新) 文档保存在我的网站——软件知识原创基地上(www.umlonline.org),请放心下载. 1)软件设计是怎样炼成的?(2014-4-1 发 ...
- 干货!IT小伙伴们实用的网站及工具大集合!持续更新!
1.Git 还在担心自己辛辛苦苦写的代码被误删了吗?还在担心自己改错了代码不能挽回吗?还在苦恼于多人开发合作找不到一个好的工具吗?那么用Git就对 了,Git是一个开源的分布式版本控制系统,用以有效. ...
随机推荐
- 3dsmax2016卸载/安装失败/如何彻底卸载清除干净3dsmax2016注册表和文件的方法
3dsmax2016提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2016失败提示3dsmax2016安装未完成,某些产品无法安装,也有时候想重新 ...
- PHP和Java中foreach循环的用法区别
1.foreach语句介绍: ①PHP: foreach 语法结构提供了遍历数组的简单方式.foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息. ...
- mysql linux下表名忽略大小写注意事项
在Unix中使用lower_case_tables_name=0,在Windows中使用lower_case_tables_name=2.这样了可以保留数据库名和表名的大小写.不利之处是必须确保在Wi ...
- maven打包报错 ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id
打开pom.xml 在build标签中 增加 <defaultGoal>compile</defaultGoal> 如下: <build><defaultGo ...
- 九度oj题目1341:艾薇儿的演唱会
题目1341:艾薇儿的演唱会(40分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:522 解决:237 题目描述: 艾薇儿今天来到了中国,她计划两天后在哈尔滨举行一场个人的演唱会.由于 ...
- ckeditor添加代码插入功能及高亮显示(插件)
Auto SyntaxHighlighter SyntaxHighlighter CKEditor Button 下载以上两个插件,启用 以下可有可无: (设置在编辑器的显示样式) ckeditor高 ...
- FocusBI: 数据仓库 (原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...
- 快速上手:在CVM上安装Apache
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由一步 发表于云+社区专栏 介绍 Apache HTTP服务器是世界上使用最广泛的Web服务器.它提供了许多强大的功能,包括可动态加载的 ...
- [java源码解析]对HashMap源码的分析(一)
最近有空的时候研究了下HashMap的源码,平时我用HashMap主要拿来当业务数据整理后的容器,一直觉得它比较灵活和好用, 这样 的便利性跟它的组成结构有很大的关系. 直接开门见山,先简要说明一下H ...
- 兼容IE和Firefox获得keyBoardEvent对象
<input type="text" name="words" id="search_txt" class="seachIp ...