//
// ViewController.m
// 05-MapView的基本使用-(掌握)
//
// Created by apple on 16/1/27.
// Copyright © 2016年 apple. All rights reserved.
//
/**
需求:
1. 显示用户位置
2. 点击大头针, 显示详情信息 --> 反地理编码
3. 切换地图类型
4. 返回用户当前位置
5. 放大和缩小(了解)
*/
#import "ViewController.h"
#import <MapKit/MapKit.h>
// MapKit包含了CoreLotion框架
// 如果使用了SB或者Xib导入MapView的时候, 需要导入框架
// MapView本身就有属性可以实现跟踪用户位置
@interface ViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) CLLocationManager *mgr;
/**
地图属性
*/
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1. 创建位置管理器并授权
self.mgr = [CLLocationManager new];
// 请求授权 --> 配置plist
if ([self.mgr respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.mgr requestWhenInUseAuthorization];
}
//2. 显示用户位置
//Tracking: 跟踪
//userTrackingMode: 用户跟踪模式
/**
MKUserTrackingModeNone = 0, // the user's location is not followed
MKUserTrackingModeFollow, // the map follows the user's location
MKUserTrackingModeFollowWithHeading, // the map follows the user's location and heading
*/
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
//3. 设置代理 --> 获取用户位置
self.mapView.delegate = self;
/**
iOS9新特性 -显示交通状况 / 显示比例尺 / 显示指南针
*/
//1.显示交通状况
self.mapView.showsTraffic = YES;
//2.显示比例尺
self.mapView.showsScale = YES;
//3.显示指南针 (默认YES)
self.mapView.showsCompass = YES;
}
#pragma mark - MapView代理方法
/**
当完成用户位置更新的时候回调用
userLocation: 显示用户位置的大头针模型
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//1. 获取经纬度
NSLog(@"la: %f, long: %f",userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude );
//2. 设置标题子标题
// userLocation.title = @"北京市";
// userLocation.subtitle = @"asasflsdafsa";
//2.1 创建Geocoder对象
CLGeocoder *geocoder = [CLGeocoder new];
//2.2 调用反地理编码方法 --> 头文件第一个方法
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//2.3 解析数据
if (placemarks.count == 0 || error) {
return;
}
CLPlacemark *placemark = placemarks.firstObject;
//locality: 城市
if (placemark.locality) {
userLocation.title = placemark.locality;
} else {
//administrativeArea: 行政区域
userLocation.title = placemark.administrativeArea;
}
//name: 详细地址
userLocation.subtitle = placemark.name;
}];
}
/**
<#content#>
*/
//- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
//{
//
//
// //1. 获取当前显示大小的跨度
// NSLog(@"latitudeDelta: %f,longitudeDelta: %f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
//
// // 放大地图: 将显示跨度缩小一倍即可
// // 放大地图: 将显示跨度放大一倍即可
//
// // 在按钮点击的时候, 先获取当前显示跨度大小, 然后按照需求放大或缩小跨度, 最后再次调用setRegion方法
//
// // latitudeDelta: 0.046142,longitudeDelta: 0.034943 首次运行完毕:
// // latitudeDelta: 0.021251,longitudeDelta: 0.016093
// // latitudeDelta: 0.010625,longitudeDelta: 0.008047
//
//}
#pragma mark - 其他方法
#pragma mark 切换地图类型
- (IBAction)changeMapTypeClick:(UISegmentedControl *)sender {
/**
MKMapTypeStandard = 0, 普通
MKMapTypeSatellite, 卫星
MKMapTypeHybrid, 混合
下面2个属性是iOS9新增的类型, 但是中国区无用
MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
一般不会使用卫星这种类型, 没什么用
*/
switch (sender.selectedSegmentIndex) {
case 0:
self.mapView.mapType = MKMapTypeStandard;
break;
case 1:
self.mapView.mapType = MKMapTypeSatellite;
break;
case 2:
self.mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
#pragma mark 返回用户的位置
- (IBAction)backUserLocationClick:(id)sender {
// 设置地图的中心点经纬度 --> 没有动画
//self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
// 设置地图的中心点经纬度 --> 有动画
//[self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
// 设置地图的中心点经纬度并且改变地图的显示跨度
//1. 中心点坐标.
CLLocationCoordinate2D centerCoordinate = self.mapView.userLocation.location.coordinate;
//2. 显示跨度大小 1°约等于111KM
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
//3. 设置Region属性 --> 没有动画
//self.mapView.region = MKCoordinateRegionMake(centerCoordinate, span);
// 调用set方法来增加动画
[self.mapView setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:YES];
}
#pragma mark 放大地图
//在按钮点击的时候, 先获取当前显示跨度大小, 然后按照需求放大或缩小跨度, 最后再次调用setRegion方法
- (IBAction)zoomInClick:(id)sender {
//1. 将当前的显示跨度缩小一倍
//region : 是当前地图显示的区域
CGFloat latitude = self.mapView.region.span.latitudeDelta * 0.5;
CGFloat longitude = self.mapView.region.span.longitudeDelta * 0.5;
//2. 设置 region
// 设置范围, 带动画
[self.mapView setRegion: MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(latitude, longitude)) animated:YES];
}
#pragma mark 缩小地图
- (IBAction)zoomOutClick:(id)sender {
//1. 将当前的显示跨度放大一倍
//region : 是当前地图显示的区域
CGFloat latitude = self.mapView.region.span.latitudeDelta * 2;
CGFloat longitude = self.mapView.region.span.longitudeDelta * 2;
// 经纬度: 经360 纬180
// 苹果地图能显示的最大跨度147 左右
if (latitude > 140 || longitude > 140) {
return;
}
//2. 设置 // 设置范围, 带动画
[self.mapView setRegion: MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(latitude, longitude)) animated:YES];
}
@end