美文网首页iOSiOS开发资源编码里程
使用苹果原生MapKit定位

使用苹果原生MapKit定位

作者: AlinCoder | 来源:发表于2015-06-02 13:28 被阅读10923次

    使用地图控件

    新建项目,在StoryBoard中拖入一个MapKit View

    直接运行,会报错

    'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named MKMapView'

    原因:没有导入MapKit框架
    点击项目->General->Targets->Linked Framework and Libraries
    加入 MapKit.framework框架,运行成功

    MapKit默认使用高德地图

    显示用户的位置

    显示用户的信息,需要设置追踪模式
    三种追踪模式:

    typedef NS_ENUM(NSInteger, MKUserTrackingMode) {
        MKUserTrackingModeNone = 0, // 不追踪
        MKUserTrackingModeFollow, // 追踪              
        MKUserTrackingModeFollowWithHeading, // 追踪位置和方向
    } NS_ENUM_AVAILABLE(NA, 5_0);
    
    

    viewController.m

    #import "ViewController.h"
    #import <MapKit/MapKit.h>
    
    @interface ViewController () <MKMapViewDelegate>
    
    // 地图
    @property (weak, nonatomic) IBOutlet MKMapView *mapKit;
    
    // 这个属性主要用来请求权限
    @property (strong, nonatomic) CLLocationManager *mgr;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 设置追踪模式
        self.mapKit.userTrackingMode = MKUserTrackingModeFollow;
        
        // 请求权限
        // 这个方法是ios8推出的,ios7没有这个方法,在ios7上执行这个方法会出错!
        // 通过检查是否存在这个方法就可以判断是ios7还是ios8
        if ([self.mgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.mgr requestAlwaysAuthorization];
        }
    }
    
    #pragma mark - 懒加载
    - (CLLocationManager *)mgr {
        if (_mgr == nil) {
            _mgr = [[CLLocationManager alloc] init];
        }
        return _mgr;
    }
    @end
    
    

    运行,会出现以下提示:

    Trying to start MapKit location updates without prompting for location authorization. Must call [CLLocationManager requestWhenInUseAuthorization] or [CLLocationManager requestAlwaysAuthorization] first.

    原因:ios8访问用户的位置信息,需要用户的授权。
    在info.plist中,添加一个字段NSLocationAlwaysUsageDescription,它的值就是请求授权时候的描述信息,可以设置也可以为空。
    当程序请求用户授权时,会显示的相应的描述信息。

    点击允许,就开始定位了!

    显示位置信息

    设置代理,实现代理方法

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        
        // 反地理编码;根据经纬度查找地名
        [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks.count == 0 || error) {
                NSLog(@"找不到该位置");
                return;
            }
            
            // 当前地标
            CLPlacemark *pm = [placemarks firstObject];
            
           // 区域名称
            userLocation.title = pm.locality;
           // 详细名称
            userLocation.subtitle = pm.name;
        }];
    }
    
    

    相关文章

      网友评论

      • 08e77db408f7:我用swift开发,用iOS9的手机测试,设置NSLocationAlwaysUsageDescription这个参数没有作用,不会弹出授权窗口为什么
        突突突突击大队:@懵然中寻找光芒 你解决了吗 兄弟
      • 奔跑的码农:在iOS8之前不是加了也没用么,还是获取不到位置信息,后面都改成locationmanger
      • 董军1990:大神求抱腿

      本文标题:使用苹果原生MapKit定位

      本文链接:https://www.haomeiwen.com/subject/sdrdqttx.html