#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
// 创建一个地图视图
*/
MKMapView *mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.mapView = mapView;
/**
创建一个UISegmentedControl, 来切换地图模式
*/
// UISegmentedControl *seg = [[UISegmentedControl alloc]initWithFrame:CGRectMake(20,70, 150, 30)];
// 一句话直接创建
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"地图",@"卫星",@"混合"]];
seg.frame = CGRectMake(100, 80, 150, 30);
seg.selectedSegmentIndex = 0; // 默认选中0
// 给一个点击事件方法,切换视图 ,
[seg addTarget:self action:@selector(segbtnclick:) forControlEvents:UIControlEventValueChanged];
/**
// 再添加一个按钮, 返回原来的地方
*/
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(100, self.view.bounds.size.height - 50, 50, 30);
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
/**
* 添加一个放大按钮
*/
UIButton *big = [[UIButton alloc]initWithFrame:CGRectMake(100, self.view.bounds.size.height - 90, 50, 30)];
[big setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[big setTitle:@"放大" forState:UIControlStateNormal];
[big addTarget:self action:@selector(bigClick) forControlEvents:UIControlEventTouchUpInside];
/**
* 添加一个缩小按钮
*/
UIButton *small = [[UIButton alloc]initWithFrame:CGRectMake(200, self.view.bounds.size.height - 90, 50, 30)];
[small setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[small setTitle:@"缩小" forState:UIControlStateNormal];
[small addTarget:self action:@selector(smallClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapView];
[self.view addSubview:seg];
[self.view addSubview:btn];
[self.view addSubview:big];
[self.view addSubview:small];
// 1 创建一个管理者
self.manager = [[CLLocationManager alloc]init];
// 2 进行授权
[self.manager requestAlwaysAuthorization];
// 3 跟踪用户位置
mapView.userTrackingMode = MKUserTrackingModeFollow; // 总是跟踪
// 4 设置代理
mapView.delegate = self;
}
#pragma mark 跟踪到用户位置回来到此方法
/**
* 跟踪用户实时位置
*
* @param mapView
* @param userLocation 大头针模型类,
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"%f--%f",userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// 设置标题和子标题
userLocation.title = @"哈哈";
userLocation.subtitle = @"子标题";
// 还能获取范围, 区域, 显示范围
NSLog(@"fanwei-%f-- %f",mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
// 获取中心位置
NSLog(@"center-%f--%f",mapView.region.center.latitude,mapView.region.center.longitude);
}
// 实现点击seg的方法
- (void)segbtnclick:(UISegmentedControl *)seg{
NSInteger sel = seg.selectedSegmentIndex;
switch (sel) {
case 0:
self.mapView.mapType = MKMapTypeStandard; // 默认
break;
case 1:
self.mapView.mapType = MKMapTypeSatellite;// 卫星
break;
case 2:
self.mapView.mapType = MKMapTypeHybrid; // 混合
break;
default:
break;
}
}
// 点击返回,, 就回到原来的位置, 并且给个视图大小
- (void)btnClick{
NSLog(@"按钮点击");
//
// CLLocationCoordinate2D center = CLLocationCoordinate2DMake(38, 108);
// [self.mapView setCenterCoordinate:center animated:YES];
CLLocationCoordinate2D center1 = CLLocationCoordinate2DMake(34.5, 115);
MKCoordinateSpan span = MKCoordinateSpanMake(80, 80);
//
[self.mapView setRegion:MKCoordinateRegionMake(center1, span) animated:YES];
}
// 点击放大, 说白了就是中心不变, 改变span值
- (void)bigClick{
// NSLog(@"放大");
//1.1 现获取span
MKCoordinateSpan span = self.mapView.region.span;
//1.2 改变
span.latitudeDelta -= 1; // 每次缩小范围
span.longitudeDelta -= 1;
// 1.3 做个判断防止崩溃
if (span.latitudeDelta < 0.01) {
return;
}
if (span.longitudeDelta < 0.01) {
return;
}
//1.3 重新赋值
self.mapView.region = MKCoordinateRegionMake(self.mapView.region.center, span);
}
// 缩小,
- (void)smallClick{
MKCoordinateSpan span = self.mapView.region.span;
span.longitudeDelta += 1;
span.latitudeDelta += 1;
// 同时支持点语法
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, span)];
}
#pragma mark region改变的时候
// 改变region,
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
// 打印一下
NSLog(@"%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}
@end
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
// 创建一个地图视图
*/
MKMapView *mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.mapView = mapView;
/**
创建一个UISegmentedControl, 来切换地图模式
*/
// UISegmentedControl *seg = [[UISegmentedControl alloc]initWithFrame:CGRectMake(20,70, 150, 30)];
// 一句话直接创建
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"地图",@"卫星",@"混合"]];
seg.frame = CGRectMake(100, 80, 150, 30);
seg.selectedSegmentIndex = 0; // 默认选中0
// 给一个点击事件方法,切换视图 ,
[seg addTarget:self action:@selector(segbtnclick:) forControlEvents:UIControlEventValueChanged];
/**
// 再添加一个按钮, 返回原来的地方
*/
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(100, self.view.bounds.size.height - 50, 50, 30);
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
/**
* 添加一个放大按钮
*/
UIButton *big = [[UIButton alloc]initWithFrame:CGRectMake(100, self.view.bounds.size.height - 90, 50, 30)];
[big setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[big setTitle:@"放大" forState:UIControlStateNormal];
[big addTarget:self action:@selector(bigClick) forControlEvents:UIControlEventTouchUpInside];
/**
* 添加一个缩小按钮
*/
UIButton *small = [[UIButton alloc]initWithFrame:CGRectMake(200, self.view.bounds.size.height - 90, 50, 30)];
[small setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[small setTitle:@"缩小" forState:UIControlStateNormal];
[small addTarget:self action:@selector(smallClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapView];
[self.view addSubview:seg];
[self.view addSubview:btn];
[self.view addSubview:big];
[self.view addSubview:small];
// 1 创建一个管理者
self.manager = [[CLLocationManager alloc]init];
// 2 进行授权
[self.manager requestAlwaysAuthorization];
// 3 跟踪用户位置
mapView.userTrackingMode = MKUserTrackingModeFollow; // 总是跟踪
// 4 设置代理
mapView.delegate = self;
}
#pragma mark 跟踪到用户位置回来到此方法
/**
* 跟踪用户实时位置
*
* @param mapView
* @param userLocation 大头针模型类,
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"%f--%f",userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// 设置标题和子标题
userLocation.title = @"哈哈";
userLocation.subtitle = @"子标题";
// 还能获取范围, 区域, 显示范围
NSLog(@"fanwei-%f-- %f",mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
// 获取中心位置
NSLog(@"center-%f--%f",mapView.region.center.latitude,mapView.region.center.longitude);
}
// 实现点击seg的方法
- (void)segbtnclick:(UISegmentedControl *)seg{
NSInteger sel = seg.selectedSegmentIndex;
switch (sel) {
case 0:
self.mapView.mapType = MKMapTypeStandard; // 默认
break;
case 1:
self.mapView.mapType = MKMapTypeSatellite;// 卫星
break;
case 2:
self.mapView.mapType = MKMapTypeHybrid; // 混合
break;
default:
break;
}
}
// 点击返回,, 就回到原来的位置, 并且给个视图大小
- (void)btnClick{
NSLog(@"按钮点击");
//
// CLLocationCoordinate2D center = CLLocationCoordinate2DMake(38, 108);
// [self.mapView setCenterCoordinate:center animated:YES];
CLLocationCoordinate2D center1 = CLLocationCoordinate2DMake(34.5, 115);
MKCoordinateSpan span = MKCoordinateSpanMake(80, 80);
//
[self.mapView setRegion:MKCoordinateRegionMake(center1, span) animated:YES];
}
// 点击放大, 说白了就是中心不变, 改变span值
- (void)bigClick{
// NSLog(@"放大");
//1.1 现获取span
MKCoordinateSpan span = self.mapView.region.span;
//1.2 改变
span.latitudeDelta -= 1; // 每次缩小范围
span.longitudeDelta -= 1;
// 1.3 做个判断防止崩溃
if (span.latitudeDelta < 0.01) {
return;
}
if (span.longitudeDelta < 0.01) {
return;
}
//1.3 重新赋值
self.mapView.region = MKCoordinateRegionMake(self.mapView.region.center, span);
}
// 缩小,
- (void)smallClick{
MKCoordinateSpan span = self.mapView.region.span;
span.longitudeDelta += 1;
span.latitudeDelta += 1;
// 同时支持点语法
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, span)];
}
#pragma mark region改变的时候
// 改变region,
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
// 打印一下
NSLog(@"%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}
@end