iOS 地图

时间:2023-03-10 07:27:46
iOS 地图
MapKit框架使用前提
导入框架 导入主头文件
#import <MapKit/MapKit.h> MapKit框架使用须知
MapKit框架中所有数据类型的前缀都是MK
MapKit有一个比较重要的UI控件 :MKMapView,专门用于地图显示 跟踪显示用户的位置 设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置
MKUserTrackingModeNone :不跟踪用户的位置
MKUserTrackingModeFollow :跟踪并在地图上显示用户的当前位置
MKUserTrackingModeFollowWithHeading :跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转 下图是跟踪效果
蓝色发光圆点就是用户的当前位置
蓝色发光原点,专业术语叫做“大头针” 可以通过设置MKMapView的mapViewType设置地图类型
MKMapTypeStandard :普通地图(左图)
MKMapTypeSatellite :卫星云图 (中图)
MKMapTypeHybrid :普通地图覆盖于卫星云图之上
MKMapView的代理

MKMapView可以设置一个代理对象,用来监听地图的相关行为

常见的代理方法有
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;
一个位置更改默认只会调用一次,不断监测用户的当前位置
每次调用,都会把用户的最新位置(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; 设置地图的显示区域
@property (nonatomic) MKCoordinateRegion region;
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated; MKCoordinateRegion MKCoordinateRegion是一个用来表示区域的结构体,定义如下
typedef struct {
CLLocationCoordinate2D center; // 区域的中心点位置
MKCoordinateSpan span; // 区域的跨度
} MKCoordinateRegion; MKCoordinateSpan的定义
typedef struct {
CLLocationDegrees latitudeDelta; // 纬度跨度
CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;
大头针的基本操作
添加一个大头针
- (void)addAnnotation:(id <MKAnnotation>)annotation; 添加多个大头针
- (void)addAnnotations:(NSArray *)annotations; 移除一个大头针
- (void)removeAnnotation:(id <MKAnnotation>)annotation; 移除多个大头针
- (void)removeAnnotations:(NSArray *)annotations; (id <MKAnnotation>)annotation参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据
大头针模型
新建一个大头针模型类
#import <MapKit/MapKit.h> @interface MyAnnotation : NSObject <MKAnnotation>
/** 坐标位置 */
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 子标题 */
@property (nonatomic, copy) NSString *subtitle;
@end 添加大头针 MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = @"传智播客iOS学院";
anno.subtitle = @"全部课程15折,会员20折,老学员30折";
anno.coordinate = CLLocationCoordinate2DMake(, );
[self.mapView addAnnotation:anno];
自定义大头针

如何自定义大头针
设置MKMapView的代理
实现下面的代理方法,返回大头针控件
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
根据传进来的(id <MKAnnotation>)annotation参数创建并返回对应的大头针控件 代理方法的使用注意
如果返回nil,显示出来的大头针就采取系统的默认样式
标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法
因此,需要在代理方法中分清楚(id <MKAnnotation>)annotation参数代表自定义的大头针还是蓝色发光圆点 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// 判断annotation的类型
if (![annotation isKindOfClass:[MJTuangouAnnotation class]]) return nil; // 创建MKAnnotationView
static NSString *ID = @"tuangou";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
annoView.canShowCallout = YES;
}
// 传递模型数据
annoView.annotation = annotation; // 设置图片
MJTuangouAnnotation *tuangouAnnotation = annotation;
annoView.image = [UIImage imageNamed:tuangouAnnotation.icon]; return annoView;
} MKAnnotationView 地图上的大头针控件是MKAnnotationView MKAnnotationView的属性
@property (nonatomic, strong) id <MKAnnotation> annotation;
大头针模型 @property (nonatomic, strong) UIImage *image;
显示的图片 @property (nonatomic) BOOL canShowCallout;
是否显示标注 @property (nonatomic) CGPoint calloutOffset;
标注的偏移量 @property (strong, nonatomic) UIView *rightCalloutAccessoryView;
标注右边显示什么控件 @property (strong, nonatomic) UIView *leftCalloutAccessoryView;
标注左边显示什么控件 MKPinAnnotationView
MKPinAnnotationView是MKAnnotationView的子类 MKPinAnnotationView比MKAnnotationView多了2个属性
@property (nonatomic) MKPinAnnotationColor pinColor;
大头针颜色 @property (nonatomic) BOOL animatesDrop;
大头针第一次显示时是否从天而降

注意:如果在storyBoard中使用了MKMapView项目中需要手动导入MpKit框架

//
// ViewController.m
// 03-地图的基本使用
//
// Created by apple on 15/1/30.
// Copyright (c) 2015年 apple. All rights reserved.
// #import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController () <MKMapViewDelegate> // 显示地图的View
@property (weak, nonatomic) IBOutlet MKMapView *mapView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 设置代理
self.mapView.delegate = self; // 跟踪用户的位置
self.mapView.userTrackingMode = MKUserTrackingModeFollow; // 设置地图类型
self.mapView.mapType = MKMapTypeSatellite;
} /**
* 定位到用户的位置会执行该方法
*
* @param userLocation 大头针模型
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 取出用户的位置
CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude); // 改变大头针显示的内容(通过改变大头针模型的属性)
// userLocation.title = @"广州市";
// userLocation.subtitle = @"广东省广州市天河区";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = userLocation.location;
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == || error) return; CLPlacemark *pm = [placemarks firstObject]; if (pm.locality) {
userLocation.title = pm.locality;
} else {
userLocation.title = pm.administrativeArea;
}
userLocation.subtitle = pm.name;
}];
} @end
//
// ViewController.m
// 03-地图的基本使用
//
// Created by apple on 15/1/30.
// Copyright (c) 2015年 apple. All rights reserved.
// #import "ViewController.h"
#import <MapKit/MapKit.h> #define kLatitudeDelta 0.002703
#define kLongitudeDelta 0.001717 @interface ViewController () <MKMapViewDelegate> // 显示地图的View
@property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (nonatomic,strong) CLLocationManager *mgr;
/**
* 点击之后回到用户的位置
*/
- (IBAction)backToUserLocation; @end @implementation ViewController - (CLLocationManager *)mgr{
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init]; [_mgr requestAlwaysAuthorization];
}
return _mgr;
} - (void)viewDidLoad {
[super viewDidLoad]; #warning 在iOS8中需要使用CLLocationManager requestAlwaysAuthorization设置后才能定位
self.mgr; // 设置代理
self.mapView.delegate = self; // 跟踪用户的位置
self.mapView.userTrackingMode = MKUserTrackingModeFollow; // 设置地图类型
// self.mapView.mapType = MKMapTypeSatellite;
} /**
* 定位到用户的位置会执行该方法
*
* @param userLocation 大头针模型
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// 取出用户的位置
CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude); // 设置mapView显示的位置
// [mapView setCenterCoordinate:coordinate animated:YES];
// 设置mapView的显示区域
MKCoordinateSpan span = MKCoordinateSpanMake(kLatitudeDelta, kLongitudeDelta);
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
[mapView setRegion:region animated:YES];
}

/**
* 区域改变的时候会调用(拖动地图)
*
* @param mapView <#mapView description#>
* @param animated <#animated description#>
*/

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
MKCoordinateRegion region = mapView.region;
CLLocationCoordinate2D center = region.center;
MKCoordinateSpan span = region.span;
NSLog(@"纬度:%f 经度:%f", center.latitude, center.longitude);
NSLog(@"纬度跨度:%f 经度跨度:%f", span.latitudeDelta, span.longitudeDelta);
} //- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
//{
//
//} - (IBAction)backToUserLocation {
MKCoordinateSpan span = MKCoordinateSpanMake(kLatitudeDelta, kLongitudeDelta);
MKCoordinateRegion region = MKCoordinateRegionMake(self.mapView.userLocation.location.coordinate, span);
// [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
[self.mapView setRegion:region animated:YES];
}
@end

ios8 地图需要代码控制授权

 
#import "ViewController.h"
#import <MapKit/MapKit.h> @interface ViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; // 请求定位需要使用CLLocationManager,所以这里需要创建一个CLLocationManager对象(iOS8)
@property(nonatomic,strong)CLLocationManager *mgr; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.mapView.delegate = self;
self.mapView.userTrackingMode = MKUserTrackingModeFollow; // 如果是iOS8,需要请求授权方式(进行判断,否则在iOS7会崩溃,需要先在info.plist中配置)
从iOS 8开始,用户定位分两种情况
总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription
if ([self.mgr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.mgr requestAlwaysAuthorization];
}
} - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"拿到用户的位置");
} - (CLLocationManager *)mgr
{
if (_mgr == nil) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
} @end