使用MapKit框架

简介:

使用MapKit框架

 

 

地图显示

最简单显示地图的代码:

//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 初始化地图控件
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    
    // 地图的类型
    mapView.mapType = MKMapTypeStandard;
    
    // 视图的宽度和高度将和父视图的宽度一起成比例变化
    mapView.autoresizingMask = \
        UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    // 显示地图
    [self.view addSubview:mapView];
}

@end

RootViewController

注意:使用地图之前是需要引入MapKit框架的哦.

autoresizingMask是干什么用的呢(实际上,我看过后还是不懂)?

UIViewAutoresizingNone

这个常量如果被设置,视图将不进行自动尺寸调整。

UIViewAutoresizingFlexibleHeight

这个常量如果被设置,视图的高度将和父视图的高度一起成比例变化。否则,视图的高度将保持不变。

UIViewAutoresizingFlexibleWidth

这个常量如果被设置,视图的宽度将和父视图的宽度一起成比例变化。否则,视图的宽度将保持不变。

UIViewAutoresizingFlexibleLeftMargin

这个常量如果被设置,视图的左边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的左边界的相对位置将保持不变。

UIViewAutoresizingFlexibleRightMargin

这个常量如果被设置,视图的右边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的右边界的相对位置将保持不变。

UIViewAutoresizingFlexibleBottomMargin

这个常量如果被设置,视图的底边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的底边界的相对位置将保持不变。

UIViewAutoresizingFlexibleTopMargin

这个常量如果被设置,视图的上边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的上边界的相对位置将保持不变。

  RootViewController.m

 

 

位置定位

//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 判断定位功能是否可以使用
    if([CLLocationManager locationServicesEnabled])
    {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        // 开始定位
        [_locationManager startUpdatingLocation];
    }
    else
    {
        NSLog(@"定位功能不可用");
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];
    NSLog(@"%@", newLocation);
    
    // 定位结束
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
    
    // 定位结束
    [manager stopUpdatingLocation];
}

@end

RootViewController.m

为什么需要判断定位功能可不可用呢?下图可以看出为什么了.

打印信息:

2014-05-15 09:25:11.883 CoreLocation[17610:60b] <+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 5/15/14, 9:25:11 AM China Standard Time

本人将这个代理定位的方式改写成了可以使用block的方式:

YXLocation.h

//
//  YXLocation.h
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@protocol YXLocationProtocol <NSObject>

@optional
- (void)currentLocation:(CLLocation *)location sucess:(BOOL)sucess;

@end

typedef void (^locationBlock_t)(CLLocation *currentLocation, BOOL sucess);

@interface YXLocation : NSObject

@property (nonatomic, copy,  readwrite)  locationBlock_t         locationBlock;
@property (nonatomic, assign, readwrite) id<YXLocationProtocol>  protocol;

@property (nonatomic, strong, readonly)  CLLocation             *location;

- (void)start;

@end

Code

YXLocation.m
//
//  YXLocation.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "YXLocation.h"

@interface YXLocation ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation YXLocation

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
    }
    return self;
}

- (void)start
{
    if([CLLocationManager locationServicesEnabled])
    {
        [_locationManager startUpdatingLocation];
    }
    else
    {
        NSLog(@"定位功能没有开启");
        
        if (_locationBlock)
        {
            _locationBlock(nil, NO);
        }
        
        if (_protocol)
        {
            [_protocol currentLocation:nil sucess:NO];
        }
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];
    
    _location = newLocation;
    
    if (_locationBlock)
    {
        _locationBlock(newLocation, YES);
    }
    
    if (_protocol)
    {
        [_protocol currentLocation:newLocation sucess:YES];
    }
    
    [_locationManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"定位功能出错");
    
    if (_locationBlock)
    {
        _locationBlock(nil, NO);
    }
    
    if (_protocol)
    {
        [_protocol currentLocation:nil sucess:NO];
    }
}

@end

Code

将经纬度转换为有意义的地址


//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()<CLLocationManagerDelegate>

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

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 判断定位功能是否可以使用
    if([CLLocationManager locationServicesEnabled])
    {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        // 开始定位
        [_locationManager startUpdatingLocation];
    }
    else
    {
        NSLog(@"定位功能不可用");
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];

    // 初始化经纬度解析器
    _geocoder = [[CLGeocoder alloc] init];
    
    // 解析经纬度值
    [_geocoder reverseGeocodeLocation:newLocation
                    completionHandler:^(NSArray *placemarks, NSError *error) {
                        CLPlacemark *placemark = [placemarks objectAtIndex:0];
                        
                        // 将字典信息拼接起来
                        NSString *locatedAt = \
                            [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                        
                        // 打印信息
                        NSLog(@"我在 %@",locatedAt);
                        
                        NSLog(@"国家代码 %@",placemark.ISOcountryCode);
                        NSLog(@"国家    %@",placemark.country);
                        NSLog(@"邮政编码 %@",placemark.postalCode);
                        NSLog(@"administrativeArea %@",placemark.administrativeArea);
                        NSLog(@"locality %@",placemark.locality);
                        NSLog(@"subLocality %@",placemark.subLocality);
                        NSLog(@"subThoroughfare %@",placemark.subThoroughfare);
                    }];
    
    // 定位结束
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
    
    // 定位结束
    [manager stopUpdatingLocation];
}

@end

RootViewController.m

打印信息如下:

2014-05-15 09:40:13.982 CoreLocation[2482:60b] 我在 中国北京市东城区东四街道东四朝阳门北小街2-1号
2014-05-15 09:40:13.986 CoreLocation[2482:60b] 国家代码 CN
2014-05-15 09:40:13.987 CoreLocation[2482:60b] 国家    中国
2014-05-15 09:40:13.988 CoreLocation[2482:60b] 邮政编码 (null)
2014-05-15 09:40:13.989 CoreLocation[2482:60b] administrativeArea 北京市
2014-05-15 09:40:13.991 CoreLocation[2482:60b] locality (null)
2014-05-15 09:40:13.992 CoreLocation[2482:60b] subLocality 东城区
2014-05-15 09:40:13.993 CoreLocation[2482:60b] subThoroughfare 2-1号

 

 

将有意义的地址转换为经纬度


//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _geocoder = [[CLGeocoder alloc] init];
    
    [_geocoder geocodeAddressString:@"中国北京市东城区东四街道东四朝阳门北小街2-1号"
                  completionHandler:^(NSArray *placemarks, NSError *error)
     {
         
         if([placemarks count] > 0 && error == nil)
         {
             
             NSLog(@"发现了 %lu placemark(s).",(unsigned long)[placemarks count]);
             
             CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
             
             NSLog(@"经度 = %f",firstPlacemark.location.coordinate.longitude);
             NSLog(@"纬度 = %f",firstPlacemark.location.coordinate.latitude);
             
         }
         else if ([placemarks count] == 0 && error == nil)
         {
             NSLog(@"没有找到 placemarks.");
             
         }
         else if (error != nil)
         {
             NSLog(@"错误 = %@",error);
         }
     }];
}

@end

RootViewController.m

打印信息:

2014-05-15 09:51:15.270 CoreLocation[2525:60b] 发现了 2 placemark(s).
2014-05-15 09:51:15.274 CoreLocation[2525:60b] 经度 = 116.425960
2014-05-15 09:51:15.275 CoreLocation[2525:60b] 纬度 = 39.931609

 

 

直接显示用户当前位置


//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // 判断定位功能是否可以使用
    if([CLLocationManager locationServicesEnabled])
    {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        // 开始定位
        [_locationManager startUpdatingLocation];
    }
    else
    {
        NSLog(@"定位功能不可用");
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // 获取坐标信息
    CLLocation *newLocation = [locations lastObject];
    
    // 定位结束
    [manager stopUpdatingLocation];
    
    // 初始化地图控件
    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    
    // 地图的类型
    mapView.mapType = MKMapTypeStandard;
    
    // 视图的宽度和高度将和父视图的宽度一起成比例变化
    mapView.autoresizingMask = \
    UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    // 显示用户所在位置(此处系统会询问你是否使用当前位置)
    mapView.showsUserLocation = YES;
    
    // 地图缩放级别
    MKCoordinateSpan span = {0.02, 0.02};
    
    // 被显示的区域
    MKCoordinateRegion region = {newLocation.coordinate, span};
    
    // 设置显示的区域
    [mapView setRegion:region];
    
    // 显示地图
    [self.view addSubview:mapView];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
    
    // 定位结束
    [manager stopUpdatingLocation];
}

@end

RootViewController.m

 

 

 


目录
相关文章
|
1月前
|
前端开发 JavaScript
框架
框架
13 3
|
7月前
|
机器学习/深度学习 计算机视觉
AIGM 框架
AIGM (Adaptive Image Generation and Manipulation) 是一个基于深度学习的图像生成和处理框架。它使用先进的生成对抗网络 (GAN) 和变分自编码器 (VAE) 技术,可以实现图像的自动生成、转换、编辑和增强等功能。
141 8
|
8月前
|
SQL XML 前端开发
1.1 初识框架
思考:框架是什么?我们为什么要学习框架呢?“框架(Framework)”一词最早出现在建筑领域,指的是在建造房屋前期构建的建筑骨架。在编程领域,框架就是应用程序的骨架,开发人员可以在这个骨架上加入自己的东西,搭建出符合自己需求的应用系统。实际开发中,随着业务的发展,软件系统变得越来越复杂,如果所有的软件都从底层功能开始开发,那将是一个漫长而繁琐的过程。此外,团队协作开发时,由于没有统一的调用规范,系统会出现大量的重复功能的代码,给系统的二次开发和维护带来不便。为解决上述问题,框架应运而生。
51 0
|
8月前
|
存储 Java 应用服务中间件
|
9月前
v4l2框架
v4l2框架
79 0
|
9月前
|
IDE Linux 开发工具
C++之openFrameworks框架
openFrameworks(简称 oF)是一个基于C++的开源库。 它提供了丰富的功能库和工具,用于快速开发多媒体、交互性和艺术创作相关的应用程序,如艺术装置、互动艺术、音视频作品、实时图形等。oF 的设计目标是让创意编程变得更加简单、直观和灵活,使艺术家、设计师、创意工作者等能够利用编程进行创作和表达。oF提供了丰富的图形、音频、输入输出、计算机视觉等功能库,并支持跨平台开发,适用于Windows、Mac OSX、Linux等操作系统。oF的社区活跃,有大量的用户和开发者共享和贡献了各种扩展、插件和示例代码。
88 0
|
程序员 测试技术
【提高自己】正确的工作方法,形成自己的思考框架
在学习过程中,将老师的知识用脑图的形式记录下来,在这里做个分享,不足之处欢迎大家指出。
|
SQL JSON 前端开发
|
传感器
CoreMotion 框架
CoreMotion框架(一)—— 基础理论CoreMotion框架(三)—— 获取陀螺仪数据CoreMotion框架(二)—— 利用加速度计获取设备的方向CoreMotion框架(四)—— 仿摩拜贴纸小球碰撞动画 ...
940 0