#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (nonatomic , strong)CLLocationManager *manager;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.manager = [CLLocationManager new];
//应用开启时请求定位
[self.manager requestWhenInUseAuthorization];
/*
MKUserTrackingModeNone = 0, // 不跟踪
MKUserTrackingModeFollow, // 跟踪
MKUserTrackingModeFollowWithHeading __TVOS_PROHIBITED, // 带朝向跟踪
*/
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
//显示比例尺
if ([self.mapView respondsToSelector:@selector(setShowsScale:)]) {
self.mapView.showsScale = YES;
}
// 显示指南针(旋转之后才会显示指南针)
self.mapView.showsCompass = YES;
//显示交通状况
self.mapView.showsTraffic = YES;
//显示3d建筑
self.mapView.showsBuildings = YES;
//航拍模式
/*
MKMapCamera cameraLookingAtCenterCoordinate:中心点
fromDistance:离中心点距离
pitch:倾斜角度
heading:朝向
*/
self.mapView.camera = [MKMapCamera cameraLookingAtCenterCoordinate:self.mapView.userLocation.coordinate fromDistance:2000.0 pitch:60 heading:0];
}
#pragma mark
#pragma mark 定位当前位置
- (IBAction)actionLocation:(id)sender {
//返回当前用户位置有2种方式
//方法一
//不带动画效果
// self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
//带动画效果
// [self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
//方法二
/*
typedef struct {
CLLocationDegrees latitudeDelta; // 纬度跨度
CLLocationDegrees longitudeDelta; // 经度跨度
} MKCoordinateSpan;
typedef struct {
CLLocationCoordinate2D center; // 中心点
MKCoordinateSpan span; // 跨度
} MKCoordinateRegion;
*/
//定义一个坐标范围
MKCoordinateRegion region;
//范围的中心点为用户当前位置的坐标
region.center = self.mapView.userLocation.coordinate;
//跨度为地图范围的跨度
region.span = self.mapView.region.span;
[self.mapView setRegion:region animated:YES];
}
#pragma mark
#pragma mark 选择地图种类
- (IBAction)actionChooseMapType:(UISegmentedControl *)sender {
/*
MKMapTypeStandard = 0, // 标准
MKMapTypeSatellite, // 卫星
MKMapTypeHybrid, // 混合
MKMapTypeSatelliteFlyover // 卫星俯视地图,
MKMapTypeHybridFlyover // 混合俯视地图
*/
//方法1
self.mapView.mapType = sender.selectedSegmentIndex;
//方法2
// switch (sender.selectedSegmentIndex) {
// case 0:
// self.mapView.mapType = MKMapTypeStandard;
// break;
// case 1:
// self.mapView.mapType = MKMapTypeSatellite;
// break;
// case 2:
// self.mapView.mapType = MKMapTypeHybrid;
// break;
// case 3:
// self.mapView.mapType = MKMapTypeSatelliteFlyover;
// break;
// case 4:
// self.mapView.mapType = MKMapTypeHybridFlyover;
// break;
// default:
// break;
// }
}
#pragma mark
#pragma mark 放大地图
- (IBAction)actionZoomin:(id)sender {
//定义一个范围
MKCoordinateRegion region;
//取当前地图中心点为中心点
region.center = self.mapView.region.center;
//取当前地图的跨度的一半作为自定义范围
region.span.latitudeDelta = self.mapView.region.span.latitudeDelta*0.5;
region.span.longitudeDelta = self.mapView.region.span.longitudeDelta*0.5;
//设置地图显示范围
[self.mapView setRegion:region animated:YES];
}
#pragma mark
#pragma mark 缩小
- (IBAction)actionZoomout:(id)sender {
MKCoordinateRegion region;
region.center = self.mapView.region.center;
region.span.latitudeDelta = self.mapView.region.span.latitudeDelta * 2;
region.span.longitudeDelta = self.mapView.region.span.latitudeDelta * 2;
[self.mapView setRegion:region animated:YES];
}
@end