地图框架:#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、在地图上添加自定义的大头针:

    1. 需要自定义一个遵守MKAnnotation协议的NSObject类
    2. 需要在自定义的类中定义MKAnnotation里面的属性,同时也可以自行扩展属性
    3. 在控制器中使用自定义类添加大头针

  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地图的更多相关文章

  1. IOS原生地图与高德地图

    原生地图 1.什么是LBS LBS: 基于位置的服务   Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位  ...

  2. iOS原生地图开发指南续——大头针与自定义标注

    iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/23 ...

  3. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  4. iOS原生地图开发进阶——使用导航和附近兴趣点检索

    iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...

  5. iOS原生地图与高德地图的使用

    原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...

  6. iOS百度地图SDK集成详细步骤

    1.iOS百度地图下载地址 http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download 根据需要选择不同的版本  ...

  7. IOS百度地图之--->第一篇《环境配置与基本使用》

    Ios 百度地图SDK简易使用说明:http://developer.baidu.com/map/index.php?title=iossdk 先道歉:对于原来上传的Demo我很抱歉,什么都没有,也没 ...

  8. iOS百度地图简单使用详解

    iOS百度地图简单使用详解 百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰 ...

  9. 支持WEB、Android、IOS的地图解决方案

    转自原文 支持WEB.Android.IOS的地图解决方案 工具链 GIS工具集 OpenGeo Suite 包含PostGIS, GeoServer, GeoWebCache, OpenLayers ...

随机推荐

  1. cad 安装失败/出错/卸载 2018/2017/2016/2015/2013/2012

    AUTO Uninstaller 更新下载地址 1.选择CAD 2.选择版本 3.点击“开始卸载”

  2. 性能测试工具LoadRunner21-LR之Controller 常用函数

    1.事务函数: Lr_start_transaction();  //标记事务的开始 Lr_end_transaction();  //标记事务的结束,一般情况下,事务开始与结束联合使用 Lr_get ...

  3. c++ 和 matlab 下的caffe模型输入差异

    在向一个caffe模型传递输入数据的时候,要注意以下两点: 1. opencv中Mat数据在内存中的存放方式是按行存储,matlab中图像在内存中的存放方式是按列存储. 2. opencv中Mat数据 ...

  4. Keepalived & Lvs集群搭建实验

    实验拓扑图: 实验原理: Keepalived 是基于 LVS ,并与 LVS 高度融合的 LVS和keepalived的关系:lvs起的是负载均衡功能,而keepalived则是高可用(热 备)的支 ...

  5. Object.create 以及 Object.setPrototypeOf

    第一部分 Object.crate() 方法是es5中的关于原型的方法, 这个方法会使用指定的原型对象以及属性去创建一个新的对象. 语法 Object.create(proto, [ properti ...

  6. MahApps.Metro控件更換微軟視窗主題

    先來看一下微軟默認的視窗主題(左:Window)與MahApps.Metro的視窗主題(右:MetroWindow), Window   MetroWindow   MetroWindow似乎美觀多了 ...

  7. hbase常识及habse适合什么场景

    当我们对于数据结构字段不够确定或杂乱无章很难按一个概念去进行抽取的数据适合用使用什么数据库?答案是什么,如果我们使用的传统数据库,肯定留有多余的字段,10个不行,20个,但是这个严重影响了质量.并且如 ...

  8. fastjson的json字符串转List

     1.代码 gameListStr = "[{"gameId":"1","gameName":"哈哈"},{& ...

  9. SQL简单语句(增删改查)

    简单的SQL语句增删改查操作 说明: 在mysql里面亲测结果正确    用到的表(学生表:studnets) 1.创建一个学生表,(学号,姓名,性别,家庭住址) mysql> create t ...

  10. (13)JavaScript之[HTML DOM元素][JS对象]

    元素 /** * HTML DOM 元素(节点)*/ //创建新的HTML元素 var para = document.createElement('p'); var node = document. ...