/**
使用地图需要导入 MapKit
同样也需要 请求用户授权
CoreLocation 是数据类 定位信息 地理编码 反地理编码
MapKit控件 显示在屏幕上的视图
MK开头
地图:MKMapView
大头针视图:MKPinAnnotationView
确定位置(在Info.plist里面添加以下代码)
*/
- (void)viewDidLoad {
[super viewDidLoad];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"没打开");
return;
}
//不管是定位 还是使用地图 都需要授权
manager = [[CLLocationManager alloc]init];
[manager requestWhenInUseAuthorization];
//不管是定位 还是使用地图 都需要授权
//初始化地图,添加地图View
myMapView = [[MKMapView alloc]initWithFrame:self.view.frame];
//设置地图的样式
/*
MKMapTypeStandard = 0, 默认的普通样式
MKMapTypeSatellite, 卫星
MKMapTypeHybrid 鸟瞰
*/
myMapView.mapType =MKMapTypeStandard;
//设置是否允许用户旋转地图
myMapView.rotateEnabled = YES;
//设置允许放大缩小地图
myMapView.zoomEnabled = YES;
//设置是否允许地图滚动
myMapView.scrollEnabled = YES;
//设置是否有3D效果
myMapView.pitchEnabled = NO;
//设置是否显示用户的位置
myMapView.showsUserLocation = YES;
//设置用户移动地图的样式
// myMapView.userTrackingMode = MKUserTrackingModeFollow;
//设置是否显示建筑物
// Affects MKMapTypeStandard 必须在 MKMapTypeStandard有效
myMapView.showsBuildings = NO;
//是否允许显示兴趣点
// Affects MKMapTypeStandard and MKMapTypeHybrid 必须在 普通样式 鸟瞰 样式 有效
myMapView.showsPointsOfInterest = NO;
myMapView.delegate = self;
[self.view addSubview:myMapView];
UILongPressGestureRecognizer *addPoint = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addPointView:)];
[myMapView addGestureRecognizer:addPoint];
}
#pragma mark 长按手势
- (void)addPointView:(UILongPressGestureRecognizer *)sender
{
//解决长按手势 触发两次
//在手势开始的时候 实行
if (sender.state == UIGestureRecognizerStateBegan) {
//把视图上面点 (point) 转换成 地图上面的坐标
CGPoint point = [sender locationInView:myMapView];
NSLog(@"%f %f",point.x,point.y);
CLLocationCoordinate2D coordinate = [myMapView convertPoint:point toCoordinateFromView:myMapView];
// NSLog(@"")
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
[myMapView addAnnotation:pointAnnotation];
}
}
#pragma mark ------地图的代理方法
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
NSLog(@"开始加载");
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
NSLog(@"完成加载");
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
NSLog(@"加载错误:%@",error);
}
//将要开始定位 用户位置的时候 调用
- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView
{
NSLog(@"将要开始定位 用户位置的时候");
}
//已经停止定位 用户位置的时候 调用
- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView
{
NSLog(@"已经停止定位 用户位置的时候");
}
//已经更新完毕 用户位置的时候 调用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"已经更新完毕 用户位置的时候");
//设置地图的区域范围
/*
// 经纬度 变化的比例范围
typedef struct {
CLLocationDegrees latitudeDelta;
CLLocationDegrees longitudeDelta;
} MKCoordinateSpan;
// 区域范围
// 以center 为中心span为比例
typedef struct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;
*/
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
//如果使用Region的时候 用户移动 出现不断跳动的效果 可以屏蔽动画
//myMapView.region = region;
[myMapView setRegion:region animated:YES];
//把地理位置 转换成名字
//大头针
//1、数据 MKPointAnnotation 继承自 <MKAnnotation>
//2、视图 MKPinAnnotationView 继承自 MKAnnotationView
//大头针 可以理解为 表示图的cell
//使用大头针(标记)数据 可以使用系统提供的MKPointAnnotation 也可以自定义MKAnnotation
//地图添加大头针数据 addAnnotation
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
//setCoordinate 设置大头针的经纬度
[annotation setCoordinate:userLocation.location.coordinate];
//地理编码 和 反地理编码 比较耗时 (并且使用了异步的方式)所以 在添加大头针标题副标题的时候 需等 转换完毕再添加
//把定位到的用户位置 转换成地名
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
//反地理编码
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
//placemarks 数组里面 存了 城市地标
CLPlacemark *placemark = [placemarks lastObject];
NSLog(@"城市名:%@ 街道名:%@",placemark.locality,placemark.thoroughfare);
//给大头针 添加标题
annotation.title = placemark.locality;
//大头针副标题
annotation.subtitle = placemark.name;
//把大头针数据 添加到地图
[myMapView addAnnotation:annotation];
}];
NSLog(@"...");
}
//定位失败
- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"定位失败");
}
/*
坐标转点
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view;
点转坐标
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view;
区域范围转 CGRect
- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(UIView *)view;
CGRect 转 区域范围
- (MKCoordinateRegion)convertRect:(CGRect)rect toRegionFromView:(UIView *)view;
*/