美文网首页
15、地图与定位(上)

15、地图与定位(上)

作者: HQ今日磨墨 | 来源:发表于2015-08-04 11:08 被阅读74次

  关于地图定位,我们需要知道,地图信息是放在 MapKit 当中的, 定位是放在 CoreLocation 里面的。在以前的版本中你需要在设置中添加这些你需要用到的 framework 框架,现在最新版本似乎是不需要,可以直接导入了。下图是手动添加的视图:

添加framework.png

  在BLDemo3中的 第五个视图控制器类中 BLFiveViewController 导入框架:

#import <UIKit/UIKit.h>
#import <BLBaseViewController.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface BLFiveViewController:BLBaseViewController<CLLocationManagerDelegate,MKMapViewDelegate>
{
    MKMapView           *_mapView;
    UILabel             *_locationLabel;
    NSMutableArray      *_annotations;
}

@property(nonatomic, strong) CLLocationManager *locationManager;
@property(nonatomic, strong) CLLocation *currentLocation;
@property(nonatomic, strong) CLGeocoder *geocoder;

@end

  为了展示地图,首先需要生成一个地图的对象在上面:

_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, width, height - 64 - 49 -50)];
//      _mapView.showsUserLocation = YES;              一旦开启,在地图界面右上角状态栏会有一个黑色定位的小图标,显示手机当前的定位。一般不建议打开,耗电。
_mapView.delegate = self;
[self.view addSubview:_mapView];

  地图添加到 view 之上后, 需要设置一个坐标位置点(设置好地点的经纬度之后,它将显示在地图的中心点位置),设置缩放比例(两个参数,分别代表纬度和经度的缩放比例),然后上面两个数据成为设置区域方法的两个参数:

Cllocationcoordinate2D coordinate = {31.19316, 121.34221};
MKCoordinateSpan span = {0.05, 0.05};
MKCoordinateRegion region = {coordinate, span};
[_mapView setRegion:region];

  然后,需要在视图的 navigationBar 上添加几个控件,这几个控件是 UIBarButtonItem:

UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithTitle:@"定位"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(locationButtonClicked:)];
self.navigationItem.leftBarButtonItem = locationButton;

UIBarButtonItem *reverseButton = [[UIBarButtonItem alloc] initWithTitle:@"解析"
                                                                  style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(reverseButtonClicked:)];
UIBarButtonItem *flagButton = [[UIBarButtonItem alloc] initWithTitle:@"标记"
                                                               style:UIBarButtonItemStyleBordered
                                                               target:self
                                                               action:@selector(flagButtonClicked:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:reverseButton, flagButton, nil];       // 在 iOS 5 之前只能添加一个按钮

  定位的方法如下:

- (void)locationButtonClicked:(id)sender
{
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManger alloc] init];
        self.locationManager.delegate = self;
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {      // 如果系统设备 >= 8.0
        [self.locationManager requestWhenInUseAuthorization];     // authorization 授权
    } else {
    [self.locationManager startUpdatingLocation];
}

  里面有关于 ios8 之后才有的用户定位请求授权功能,如下图所示:

定位请求.jpg

  这个方法也是需要代理回调的,代码如下:

#pragma mark - CLLocationManagerDelegate methods

- (void) locationManager:(CLLocationManager *)manager didChangerAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusNotDetermined) {
        NSLog(@"等待用户授权");
    } else if (status == kCLAuthorizationStatusAuthorizedAlways ||          // 点选了允许之后就是这两个状态
               status == kCLAuthorizationStatusAutorizedWhenInUse) {
         [self.locationManager startUpdatingLocation];
    } else {
        NSLog(@"授权失败");
    }
}

  在开发中还有一个坑,当你请求用户授权,写入如上代码还是不够的,还需要到项目中,点击targets,选择项目,在info 设置中 选择设置 NSlocationWhenInUseUsageDescription ,内容value 设置为 “需要定位”,走完这个流程才能请求授权,才能定位:

定位用户授权的工程设置.jpg

  定位成功或者失败多次之后,需要停止定位 (stopUpdatingLocation),也在代理中含有方法,观察里面的一些方法代码:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    [self.locationManager stopUpdatingLocation];
    NSLog(@"%@", error.description);
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];
    self.currentLocation = newLocation;
    CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
    _locationLabel.text = [NSString stringWithFormat:@"%f, %f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
}

相关文章

  • 15、地图与定位(上)

      关于地图定位,我们需要知道,地图信息是放在 MapKit 当中的, 定位是放在 CoreLocation 里面...

  • iOS地图和定位

    iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/ 导入地图框架 1、...

  • 地图与定位

    OCiOS开发:地图与定位 - 李鴻耀 - 博客频道 - CSDN.NET iOS开发之地图-定位/...

  • 基于fabric的地图定位,SVG热力地图

    基于fabric的地图定位,SVG热力地图 基于fabric的地图定位,SVG热力地图 基于 fabricjs v...

  • 地图定位的不显示

    苹果自带地图定位功能 地图定位 今天要做苹果自带地图定位功能,基于mapkit框架的。怎么也没有找到定位自己的位置...

  • 22.被AppStore拒绝上架的原因总结

    1.APP内部使用地图定位功能,打开定位服务,从来没有关闭过。地图定位功能的打开与关闭应该是成对出现的。 2.应用...

  • 人人都能懂点VSLAM技术

    写在前面 SLAM (Simultaneous Localization And Mapping,同步定位与地图构...

  • iOS文章目录

    书架 常用框架UIKit框架:UI控件显示与事件处理UIKit-ControllerUIKit-View定位与地图...

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • 地图显示踩坑

    问题一:为什么定位点不在地图正中间? 应该先显示地图div,再画地图画地图时,先获取当前定位的坐标,再画地图 问题...

网友评论

      本文标题:15、地图与定位(上)

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