iOS MapKit地图
地图框架:#import <MapKit/MapKit.h>
基本属性和方法:
属性:
- 地图类视图:MKMapView
- 地图类型:MKMapType mapType
- 地图旋转:rotateEnabled
- 用户追踪:MKUserTrackingMode userTrackingMode
- 地图区域:MKCoordinateRegion region
- 地图代理方法:MKMapViewDelegate
- 用户位置类:MKUserLocation
- 大头针类:MKAnnotation
- 请求用户授权:requestAlwaysAuthorization
方法:
地图的区域改变完成时调用:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
每次更新到用户的位置就会调用(调用不频繁, 只有位置改变才会调用):
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
每次添加大头针就会调用(地图上有几个大头针就调用几次):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
MapKit方法的基本实现:
A、添加地图,并且更新到用户位置
#import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController ()<MKMapViewDelegate>
// 地图
@property (nonatomic,strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
// 地理编码对象
@property (nonatomic ,strong) CLGeocoder *geocoder;
@end @implementation ViewController // 懒加载 - mapView
- (MKMapView *)mapView
{
if (!_mapView){ _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
// 标准地图
/*
typedef enum : NSUInteger {
MKMapTypeStandard , 标准(默认)
MKMapTypeSatellite ,卫星
MKMapTypeHybrid 混合(标准 + 卫星)
} MKMapType;
*/
_mapView.mapType = MKMapTypeStandard; // 追踪用户
// 如果想利用MapKit获取用户的位置, 可以追踪
/*
typedef NS_ENUM(NSInteger, MKUserTrackingMode) {
MKUserTrackingModeNone = 0, 不追踪/不准确的
MKUserTrackingModeFollow, 追踪
MKUserTrackingModeFollowWithHeading, 追踪并且获取用的方向
}
*/
_mapView.userTrackingMode = MKUserTrackingModeFollow; [self.view addSubview:_mapView];
} return _mapView;
} #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
} - (void)viewDidLoad {
[super viewDidLoad]; // 注意:在iOS8中, 如果想要追踪用户的位置, 必须自己主动请求隐私权限
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
// 主动请求权限
self.mgr = [[CLLocationManager alloc] init]; [self.mgr requestAlwaysAuthorization];
} // 成为mapVIew的代理
self.MapView.delegate = self; } #pragma MKMapViewDelegate
// 每次更新到用户的位置就会调用(调用不频繁, 只有位置改变才会调用)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ // 利用反地理编码获取位置之后设置标题
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
NSLog(@"获取地理位置成功 name = %@ locality = %@", placemark.name, placemark.locality);
userLocation.title = placemark.name;
userLocation.subtitle = placemark.locality;
}]; // 移动地图到当前用户所在位置
// 获取用户当前所在位置的经纬度, 并且设置为地图的中心点
// [self.MapView setCenterCoordinate:userLocation.location.coordinate animated:YES]; // 设置地图显示的区域
// 获取用户的位置
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 指定经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.009310,0.007812);
// 将用户当前的位置作为显示区域的中心点, 并且指定需要显示的跨度范围
MKCoordinateRegion region = MKCoordinateRegionMake(center, span); // 设置显示区域
[self.MapView setRegion:region animated:YES];
} // 地图的区域改变完成时调用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
// 获取到经纬度跨度
NSLog(@"%f %f", self.MapView.region.span.latitudeDelta, self.MapView.region.span.longitudeDelta);
} @end
B、在地图上添加自定义的大头针:
- 需要自定义一个遵守MKAnnotation协议的NSObject类
- 需要在自定义的类中定义MKAnnotation里面的属性,同时也可以自行扩展属性
- 在控制器中使用自定义类添加大头针
1、自定义大头针类:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h> @interface WYSAnnotation : NSObject<MKAnnotation>
// 大头针的位置
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// 大头针标题
@property (nonatomic, copy) NSString *title;
// 大头针的子标题
@property (nonatomic, copy) NSString *subtitle; // 图标 - 自己扩展的属性
@property (nonatomic, copy) NSString *icon;
2、在ViewController中添加大头针:
// 创建大头针模型
WYSAnnotation *anno = [[HMAnnotation alloc] init];
anno.title = @"GeekStar";
anno.subtitle = @"贵在坚持";
// 大头针添加在随机经纬度位置
CGFloat latitude = 32.22 + arc4random_uniform();
CGFloat longitude = 132.48 + arc4random_uniform();
anno.coordinate = CLLocationCoordinate2DMake(latitude , longitude);
anno.icon = @"hehe.jpg"; // 添加大头针
[self.mapView addAnnotation:anno];
3、实现大头针的代理方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{ // 对用户当前的位置的大头针特殊处理,直接使用系统提供的大头针
if ([annotation isKindOfClass:[HMAnnotation class]] == NO) {
return nil;
} // 缓存
static NSString *identifier = @"anno";
//缓存池中取
// 注意: 默认情况下MKAnnotationView是无法显示的, 如果想自定义大头针可以使用MKAnnotationView的子类MKPinAnnotationView // 注意: 如果是自定义的大头针, 默认情况点击大头针之后是不会显示标题的, 需要我们自己手动设置显示
// MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// 2.如果缓存池中没有, 创建一个新的
if (annoView == nil) {
// annoView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
annoView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier]; // 设置大头针的颜色
// annoView.pinColor = MKPinAnnotationColorPurple; // 设置大头针从天而降
// annoView.animatesDrop = YES; // 设置大头针标题是否显示
annoView.canShowCallout = YES; // 设置大头针标题显示的偏移位
// annoView.calloutOffset = CGPointMake(-50, 0); // 设置大头针左边的辅助视图
annoView.leftCalloutAccessoryView = [[UISwitch alloc] init];
// 设置大头针右边的辅助视图
annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; } // 设置大头针的图片
// 注意: 如果你是使用的MKPinAnnotationView创建的自定义大头针, 那么设置图片无效, 因为系统内部会做一些操作, 覆盖掉我们自己的设置
// annoView.image = [UIImage imageNamed:@"hehe.jpg"];
HMAnnotation *anno = (HMAnnotation *)annotation;
annoView.image = [UIImage imageNamed:anno.icon]; // 3.给大头针View设置数据
annoView.annotation = annotation; // 4.返回大头针View
return annoView; }
C、ios系统自带导航:
#import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController () // 地理编码对象
@property(nonatomic, strong) CLGeocoder *geocoder;
@end @implementation ViewController #pragma mark - 懒加载
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取用户输入的起点和终点
NSString *startStr = @"上海";
NSString *endStr = @"北京"; // 地理编码
[self.geocoder geocodeAddressString:startStr completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == ) return; // 开始位置的地标
CLPlacemark *startCLPlacemark = [placemarks firstObject]; // 地理编码
[self.geocoder geocodeAddressString:endStr completionHandler:^(NSArray *placemarks, NSError *error) { if (placemarks.count == ) return; // 结束位置的地标
CLPlacemark *endCLPlacemark = [placemarks firstObject]; // 开始导航
[self startNavigationWithstartCLPlacemark:startCLPlacemark endCLPlacemark:endCLPlacemark];
}]; }];
} // 开始导航
- (void)startNavigationWithstartCLPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark
{ //创建起点
MKPlacemark *startPlacemark = [[MKPlacemark alloc] initWithPlacemark:startCLPlacemark];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPlacemark];; //创建终点
MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithPlacemark:endCLPlacemark];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark]; // 设置数组
NSArray *items = @[startItem, endItem]; // 设置属性
NSMutableDictionary *options = [NSMutableDictionary dictionary];
// 模式
options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving; // 开启系统导航
[MKMapItem openMapsWithItems:items launchOptions:options];
} @end
iOS MapKit地图的更多相关文章
- IOS原生地图与高德地图
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 ...
- iOS原生地图开发指南续——大头针与自定义标注
iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/23 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
- iOS原生地图开发进阶——使用导航和附近兴趣点检索
iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...
- iOS原生地图与高德地图的使用
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...
- iOS百度地图SDK集成详细步骤
1.iOS百度地图下载地址 http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download 根据需要选择不同的版本 ...
- IOS百度地图之--->第一篇《环境配置与基本使用》
Ios 百度地图SDK简易使用说明:http://developer.baidu.com/map/index.php?title=iossdk 先道歉:对于原来上传的Demo我很抱歉,什么都没有,也没 ...
- iOS百度地图简单使用详解
iOS百度地图简单使用详解 百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰 ...
- 支持WEB、Android、IOS的地图解决方案
转自原文 支持WEB.Android.IOS的地图解决方案 工具链 GIS工具集 OpenGeo Suite 包含PostGIS, GeoServer, GeoWebCache, OpenLayers ...
随机推荐
- javascript Array数组详解 各种方法
1.数组的声明方法(1): arrayObj = new Array(); //创建一个数组.复制代码 代码如下: var arr1 = new Array(); (2):arrayObj = new ...
- DRF-->2序列化组件的使用和接口设计--get,post,put,delete&优化组件
!!!!! !!!!! 记住这个图 !!!!! 上篇博客说道DRF序列化组件的get,只是简单的举一个实例,然而在现实生活中我们前后端进行交互的时候更多的用到了Json数据格式,这也就是说前后端交互的 ...
- (转)在 VMware 中安装 HMC
在 VMware 中安装 HMC 原文:http://blog.csdn.net/ccie38499/article/details/14123493 http://www.54it.top/arch ...
- Ajax原理实现
jQuery的ajax实现原理: // 1.创建一个XMLHttpRequest对象 var xhr = new XMLHttpRequest(); // 2.打开请求 xhr.open('METHO ...
- GitKraken使用教程-基础部分(9)
10. 合并分支并解决冲突(conflict) 1) 合并分支 在代码管理过程中,切换分支或者同步服务器代码时,常常会出现代码冲突的情况,这种情况出现的原因一般是由于两个分支对同一个文件进行修改, ...
- MyEclipse 比较常用的快捷键
Ctrl+D: 删除当前行 Alt+↓ 当前行和下面一行交互位置(特别实用,可以省去先剪切,再粘贴了) Alt+↑ 当前行和上面一行交互位置(同上) Alt+← 前一个编辑的页面 Alt+→ 下一个编 ...
- java解析json串常识
注意:JSONObject 和JSONArray的使用区别 报错:A JSONObject text must begin with '{' at character 1 of 分析: JSONOb ...
- iOS UIWebView 和 WKWebView 的 cookie 获取,设置,删除
Cookie简介说到Cookie,或许有些小伙伴会比较陌生,有些小伙伴会比较熟悉.如果项目中,所有页面都是纯原生来实现的话,一般Cookie这个东西或许我们永远也不会接触到.但是,这里还是要说一下Co ...
- 102001 E
x轴上方给你n个点,m个水平杆子, 然后q组询问,每次询问一个点,问能看到多少个点. n,q<=40000,m<=5 自闭了呀,又写了个 for(int i=1;i<(1<&l ...
- TP5.0搭建restful API 应用
1.配置环境变量,如果没配置会显示如下错误. 配置方法 1)右键此电脑-> 属性-> 高级系统设置->环境变量->Path 2)在Path后加上php目录的名称 如:E:\PH ...