地图MapKit框架

MapKit框架的使用

使用前先导入头文件 :

#import <MapKit/MapKit.h>
MapKit框架使用须知

MapKit框架中所有数据类型的前缀都是MK
MapKit有一个比较重要的UI控件 :MKMapView,专门用于地图显示

跟踪显示用户的位置

设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置
1. MKUserTrackingModeNone :不跟踪用户的位置
2. MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置
3.MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转

地图的类型

可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard :普通地图
MKMapTypeSatellite :卫星云图
MKMapTypeHybrid :普通地图覆盖于卫星云图之上

MKMapView的代理

常见的代理方法有:

调用非常频繁,不断监测用户的当前位置
每次调用,都会把用户的最新位置(userLocation参数)传进来

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;

地图的显示区域即将发生改变的时候调用

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;

地图的显示区域已经发生改变的时候调用

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
MKUserLocation

MKUserLocation其实是个大头针模型,包括以下属性
显示在大头针上的标题
@property (nonatomic, copy) NSString *title;

显示在大头针上的子标题
@property (nonatomic, copy) NSString *subtitle;

地理位置信息(大头针钉在什么地方?)
@property (readonly, nonatomic) CLLocation *location;

设置地图的显示

通过MKMapView的下列方法,可以设置地图显示的位置和区域
设置地图的中心点位置

@property (nonatomic) CLLocationCoordinate2D centerCoordinate;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;
****************************************************
// 设置地图显示的中心位置
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39, 116);
    [self.mapView setCenterCoordinate:center animated:YES];
设置地图的显示区域 MKCoordinateRegion
@property (nonatomic) MKCoordinateRegion region;
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
// MKCoordinateRegion是一个用来表示区域的结构体,定义如下
typedef struct {
        CLLocationCoordinate2D center; // 区域的中心点位置
        MKCoordinateSpan span; // 区域的跨度
} MKCoordinateRegion;

// MKCoordinateSpan的定义
typedef struct {
    CLLocationDegrees latitudeDelta; // 纬度跨度
    CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;
*******************************************************
    // 设置地图显示的区域 (跨度越小显示的越详细)
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39, 116);  
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1,0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    [self.mapView setRegion:region animated:YES];
接触完定位与显示后会发现有几种类和类的相关属性不容易区分容易,下面做一下简单的区分
/**
 CLLocation : 封装位置信息(经纬度、海拔)
 CLPlacemark : 封装地标信息(位置信息CLLocation、地名name、国家country)
 MKUserLocation : 封装地图上大头针的位置信息(位置信息CLLocation、标题title、子标题subtitle)
 CLLocationDegrees : 度数(经度、纬度)
 CLLocationCoordinate2D : 地理坐标(经度CLLocationDegrees longitude、纬度CLLocationDegrees latitude)
 MKCoordinateSpan : 跨度(经度跨度CLLocationDegrees longitudeDelta、纬度跨度CLLocationDegrees latitudeDelta)
 MKCoordinateRegion: 区域(中心位置CLLocationCoordinate2D center、区域跨度MKCoordinateSpan span)
 */

具体代码显示地图:

引入头文件,声明相关属性
#import "ViewController.h"
#import <MapKit/MapKit.h>// 导入地图头文件

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (strong, nonatomic) MKMapView *mapView;// 地图
@property (strong, nonatomic) CLLocationManager *manager;
@end
懒加载创建已声明的对象
-(MKMapView *)mapView{
    if (_mapView == nil) {
        self.mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
    }
    return _mapView;
}
-(CLLocationManager *)manager{
    if (_manager == nil) {
        self.manager = [[CLLocationManager alloc]init];
    }
    return _manager;
}
在 - (void)viewDidLoad;中设置地图
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置地图类型 (有三种类型)
    self.mapView.mapType = MKMapTypeStandard;
    // 设置跟踪模式 (三种模式)
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    // 设置代理(监控地图的相关行为:比如显示的区域发生了改变)
    self.mapView.delegate = self;
    // 定位当前位置
    self.mapView.showsUserLocation = YES;
    // 显示实时交通路况图层
    //_mapView.showTraffic = YES;

    //  请求是否允许访问位置信息 (记得去info.plist文件里添加参数)
    [self.manager requestAlwaysAuthorization];
    [self.view addSubview:self.mapView]; 
    // 此处应先创建button(暂未创建)
    [self backToUserLocation];// 点击按钮使地图回到用户位置
}
MKMapViewDelegate(代理方法)
/**
 * 更新到用户的位置时就会调用(显示的位置,显示范围改变)
 * userLocation : 大头针模型数据,对大头针位置的一个封装(这里的userLocation
 描述的是用来显示用户位置的蓝色大头针)
 当前mapView即屏幕显示内地图的信息
 */
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    userLocation.title = @"火星";
    userLocation.subtitle = @"火星科技有限公司研发部";
 // userLocation.location.coordinate.latitude;// 纬度
 // userLocation.location.coordinate.longitude;// 经度

    // 让用户的位置显示在地图中心(当用户的位置移动并跟踪时)
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    MKCoordinateSpan span = MKCoordinateSpanMake(1, 1);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate , span);
    [self.mapView setRegion:region animated:YES];

}

/**
 *  地图显示的区域即将改变了就会调用
 */
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

}

/**
 *  地图显示的区域改变了就会调用(显示的位置,显示范围改变)
 */
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    CLLocationCoordinate2D center = mapView.region.center;// 地图显示的中心点
    MKCoordinateSpan span = mapView.region.span;// 地图显示范围的经纬度跨度

    NSLog(@"中心点=(%f, %f), 区域跨度=(%f, %f)", center.longitude, center.latitude, span.longitudeDelta, span.latitudeDelta);
}

#pragma mark  点击按钮使地图回到用户位置(自定义方法)
- (void)backToUserLocation {
    CLLocationCoordinate2D center =  self.mapView.userLocation.location.coordinate;
    [self.mapView setCenterCoordinate:center animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值