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 ...
随机推荐
- mysql 8 修改root 密码
主要参考:https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html 需要注意的是创建文件的时候需要保存为 utf-8 无 B ...
- (转)轻松应对IDC机房带宽突然暴涨问题
原文:http://blog.51cto.com/oldboy/909696
- LeetCode 704.二分查找(C++)
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- React.js 小书 Lesson7 - 组件的 render 方法
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson7 转载请注明出处,保留原文链接和作者信息. React.js 中一切皆组件,用 React. ...
- HDU 5412——CRB and Queries——————【线段树套Treap(并没有AC)】
CRB and Queries Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 如何快速定位JVM中消耗CPU最多的线程?
第一步.先找出Java的进程PID ps -ef | grep 进程名关键字 这里假设找到的PID是:12345 第二步.找出该进程内最消耗CPU的线程 top -Hp log4x R :11.7 ...
- Hibernate课程 初探一对多映射3-3 单向多对一的测试
public static void testManyToOne(){ Student stu1 = new Student("小明","男"); Studen ...
- Kindeditor单独调用单图上传增加预览
html代码: <p><input type="hidden" id="url1" name="IDCardPicture1&quo ...
- 解决Pandoc wasn't found.pdflatex not found on PATH
解决nbconvert failed: Pandoc wasn't found.解决nbconvert failed: pdflatex not found on PATH 问题1描述 500 : I ...
- 使用windows资源管理器打开jar
在命令行里输入: assoc .jar=CompressedFolder