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、添加地图,并且更新到用户位置

  1 #import "ViewController.h"
  2 #import <MapKit/MapKit.h>
  3
  4 @interface ViewController ()<MKMapViewDelegate>
  5 //  地图
  6 @property (nonatomic,strong) MKMapView *mapView;
  7 @property (nonatomic, strong) CLLocationManager *mgr;
  8 // 地理编码对象
  9 @property (nonatomic ,strong) CLGeocoder *geocoder;
 10 @end
 11
 12 @implementation ViewController
 13
 14 // 懒加载 - mapView
 15 - (MKMapView *)mapView
 16 {
 17     if (!_mapView){
 18
 19         _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
 20         // 标准地图
 21           /*
 22      typedef enum : NSUInteger {
 23      MKMapTypeStandard , 标准(默认)
 24      MKMapTypeSatellite ,卫星
 25      MKMapTypeHybrid 混合(标准 + 卫星)
 26      } MKMapType;
 27      */
 28         _mapView.mapType = MKMapTypeStandard;
 29
 30
 31         // 追踪用户
 32           // 如果想利用MapKit获取用户的位置, 可以追踪
 33     /*
 34      typedef NS_ENUM(NSInteger, MKUserTrackingMode) {
 35      MKUserTrackingModeNone = 0, 不追踪/不准确的
 36      MKUserTrackingModeFollow, 追踪
 37      MKUserTrackingModeFollowWithHeading, 追踪并且获取用的方向
 38      }
 39      */
 40         _mapView.userTrackingMode =  MKUserTrackingModeFollow;
 41
 42         [self.view addSubview:_mapView];
 43     }
 44
 45     return _mapView;
 46 }
 47
 48 #pragma mark - 懒加载
 49 - (CLGeocoder *)geocoder
 50 {
 51     if (!_geocoder) {
 52         _geocoder = [[CLGeocoder alloc] init];
 53     }
 54     return _geocoder;
 55 }
 56
 57
 58 - (void)viewDidLoad {
 59     [super viewDidLoad];
 60
 61     // 注意:在iOS8中, 如果想要追踪用户的位置, 必须自己主动请求隐私权限
 62     if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
 63         // 主动请求权限
 64         self.mgr = [[CLLocationManager alloc] init];
 65
 66         [self.mgr requestAlwaysAuthorization];
 67     }
 68
 69
 70     // 成为mapVIew的代理
 71     self.customMapView.delegate = self;
 72
 73 }
 74
 75 #pragma MKMapViewDelegate
 76 //  每次更新到用户的位置就会调用(调用不频繁, 只有位置改变才会调用)
 77 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
 78 {
 79
 80     // 利用反地理编码获取位置之后设置标题
 81     [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
 82         CLPlacemark *placemark = [placemarks firstObject];
 83         NSLog(@"获取地理位置成功 name = %@ locality = %@", placemark.name, placemark.locality);
 84         userLocation.title = placemark.name;
 85         userLocation.subtitle = placemark.locality;
 86     }];
 87
 88
 89
 90     // 移动地图到当前用户所在位置
 91     // 获取用户当前所在位置的经纬度, 并且设置为地图的中心点
 92 //    [self.customMapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
 93
 94     // 设置地图显示的区域
 95     // 获取用户的位置
 96     CLLocationCoordinate2D center = userLocation.location.coordinate;
 97     // 指定经纬度的跨度
 98     MKCoordinateSpan span = MKCoordinateSpanMake(0.009310,0.007812);
 99     // 将用户当前的位置作为显示区域的中心点, 并且指定需要显示的跨度范围
100     MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
101
102     // 设置显示区域
103     [self.customMapView setRegion:region animated:YES];
104 }
105
106
107 //  地图的区域改变完成时调用
108 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
109 {
110     // 获取到经纬度跨度
111     NSLog(@"%f %f", self.customMapView.region.span.latitudeDelta, self.customMapView.region.span.longitudeDelta);
112 }
113
114 @end

  

  B、在地图上添加自定义的大头针:

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

  1、自定义大头针类:

 1 #import <Foundation/Foundation.h>
 2 #import <MapKit/MapKit.h>
 3
 4 @interface WYSAnnotation : NSObject<MKAnnotation>
 5  //  大头针的位置
 6 @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
 7 // 大头针标题
 8 @property (nonatomic, copy) NSString *title;
 9 // 大头针的子标题
10 @property (nonatomic, copy) NSString *subtitle;
11
12 //  图标 - 自己扩展的属性
13 @property (nonatomic, copy) NSString *icon;

  2、在ViewController中添加大头针:

 1   // 创建大头针模型
 2     WYSAnnotation *anno = [[HMAnnotation alloc] init];
 3     anno.title = @"GeekStar";
 4     anno.subtitle = @"贵在坚持";
 5     // 大头针添加在随机经纬度位置
 6     CGFloat latitude = 32.22 + arc4random_uniform(5);
 7     CGFloat longitude = 132.48 + arc4random_uniform(5);
 8     anno.coordinate = CLLocationCoordinate2DMake(latitude , longitude);
 9     anno.icon = @"hehe.jpg";
10
11     // 添加大头针
12     [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系统自带导航:

 1 #import "ViewController.h"
 2 #import <MapKit/MapKit.h>
 3
 4 @interface ViewController ()
 5
 6 //  地理编码对象
 7 @property(nonatomic, strong) CLGeocoder *geocoder;
 8 @end
 9
10 @implementation ViewController
11
12 #pragma mark - 懒加载
13 - (CLGeocoder *)geocoder
14 {
15     if (!_geocoder) {
16         self.geocoder = [[CLGeocoder alloc] init];
17     }
18     return _geocoder;
19 }
20
21
22 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
23 {
24     // 获取用户输入的起点和终点
25     NSString *startStr = @"上海";
26     NSString *endStr = @"北京";
27
28
29     // 地理编码
30     [self.geocoder geocodeAddressString:startStr completionHandler:^(NSArray *placemarks, NSError *error) {
31         if (placemarks.count == 0) return;
32
33         // 开始位置的地标
34         CLPlacemark *startCLPlacemark = [placemarks firstObject];
35
36
37         // 地理编码
38         [self.geocoder geocodeAddressString:endStr completionHandler:^(NSArray *placemarks, NSError *error) {
39
40             if (placemarks.count == 0) return;
41
42             // 结束位置的地标
43             CLPlacemark *endCLPlacemark = [placemarks firstObject];
44
45             // 开始导航
46             [self startNavigationWithstartCLPlacemark:startCLPlacemark endCLPlacemark:endCLPlacemark];
47         }];
48
49     }];
50 }
51
52 //  开始导航
53 - (void)startNavigationWithstartCLPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark
54 {
55
56     //创建起点
57     MKPlacemark *startPlacemark = [[MKPlacemark alloc] initWithPlacemark:startCLPlacemark];
58     MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPlacemark];;
59
60     //创建终点
61     MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithPlacemark:endCLPlacemark];
62     MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark];
63
64     // 设置数组
65     NSArray *items = @[startItem, endItem];
66
67
68     // 设置属性
69     NSMutableDictionary *options = [NSMutableDictionary dictionary];
70     // 模式
71     options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
72
73     // 开启系统导航
74     [MKMapItem openMapsWithItems:items launchOptions:options];
75 }
76
77
78 @end
时间: 2024-08-28 21:57:31

ios MapKit地图的相关文章

IOS原生地图与高德地图

原生地图 1.什么是LBS LBS: 基于位置的服务   Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位      2.基站定位      3.WIFI定位 3.框架 MapKit:地图框架,显示地图 CoreLocation:定位框架,没有地图时也可以使用定位. 4.如何使用原生地图<MapKit> 和定位<CoreLocation> MapKit: 1) 初始化MapView _map

IOS百度地图使用基础指南+原生分享&友盟分享

1.地图 1.获取用户的经纬度(CLLocationManager) 创建属性:CLLocationManager *mgr; 遵守协议:<CLLocationManagerDelegate> a>创建定位管理器 self.mgr = [[CLLocationManager alloc] init]; b>设置代理 self.mgr.delegate = self; c>开始定位 [self.mgr startUpdatingLocation]; 代理方法: -(void)l

iOS百度地图

一.百度地图sdk详细使用过程,参考下面的博客(我感觉很详细,而且有问题可以找楼主,他会给解答,感谢好心人-): 如何使用百度地图sdk,及相关demo下载 上述博客给的demo,是真机运行的(百度地图sdk区分真机和模拟器).博客中的demo,需要自己手动修改一些设置,才可运行(可对比下面的注意事项设置). 二.iOS中,使用百度地图sdk的注意事项: 1.百度地图sdk区分真机和模拟器,导入的时候需要注意: 2.工程中一定要有.mm文件: 3.一定要添加MapKit.framework.Co

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

iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更加实用的功能的开发:线路导航与兴趣点搜索.前几篇博客的链接如下: 地图基础用法详解:http://my.oschina.net/u/2340880/blog/415360. 添加大头针与自定义标注:http://my.oschina.net/u/2340880/blog/415441. 添加地图覆盖

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

iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/2340880/blog/415360系统总结了iOS原生地图框架MapKit中主体地图的设置与应用.这篇是上一篇的一个后续,总结了系统的大头针视图以及自定义标注视图的方法. 一.先来认识一个协议MKAnnotation 官方文档告诉我们,所有标注的类必须遵守这个协议.所以可以了解,标注这个概念在逻辑属

iOS MapKit导航及地理转码辅助类

Problem 2167 大王叫我来巡山呐 做的的第二题 呵呵 Problem 2168 防守阵地 I 做的第一题 打了下草稿 马上切了它 假设当前x=(ai)*1+(ai+1)*2+(ai+2)*3+''''+(aj)*m 下一次是(ai+1)*1+(ai+2)*2+(ai+3)*3+''''+(aj+1)*m = (ai)*1+(ai+1)*2+(ai+2)*3+''''+(aj)*m+(aj+1)*(m+1) -(ai+(ai+1)+(ai+2)+'''+(aj)+(aj+1)) 紫色的这

iOS百度地图poi检索(基于百度地图2.3.0SDK开发)

正好做到百度地图的poi检索,拿来分享一下,基于百度地图2.3.0SDK开发,说实话,这个新版本poi真不好用 先来看头文件里面,定义poi对象,当然你也可以写到实现文件里面 #import <UIKit/UIKit.h> #import "BMapKit.h" @interface NearByPlaceViewController : ViewControllerBase <BMKPoiSearchDelegate> {     BMKPoiSearch *

ios 百度地图使用

第一步.引入 1.下载SDK  地址http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download 2.解压出BaiduMap_IOSSDK_v2.3.0_Lib.在工程中第三方中添加文件夹BaiDumap 然后在文件夹中放入(1:inc;2:mapapi.bundle:libbaidumapapi.a) 第二步.创建百度地图应用 1.http://lbsyun.baidu.com/apiconsole/key在这

IOS 百度地图导入最新 SDK 2.9 报错

IOS 百度地图导入最新2.9SDK 报错 _baidu_framework::HandleBasemapMerge(_baidu_vi::CVString const 解决: 因为XCode默认使用的C++标准库是苹果自己的libc++(LLVM C++ standard library with c++11 support),而百度地图SDK中使用的C++标准库是GNU C++的.解决方法,在target的build settings中搜索"c++ standard library"