//
// ViewController.m
// 地图
//
// Created by zj on 16/5/22.
// Copyright © 2016年 zj. All rights reserved.
//
#import "ViewController.h"
#import
#import
#import "AnnotationModel.h"
@interface ViewController ()<MKMapViewDelegate>
{
//设置地图的全局变量
MKMapView *mapview;
//设置定位的全局变量
CLLocationManager *manager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建地图
mapview = [[MKMapView alloc]initWithFrame:self.view.frame];
[self.view addSubview:mapview];
//设置跟踪类型
mapview.userTrackingMode = MKUserTrackingModeFollowWithHeading;
//设置地图类型
mapview.mapType = MKMapTypeStandard;
//userLocation获取到当前用户位置
NSLog(@"%f ",mapview.userLocation.coordinate.latitude);
//是否显示指南针
mapview.showsCompass = NO;
//是否显示比例尺
mapview.showsScale = YES;
//是否显示交通
mapview.showsTraffic = YES;
//是否显示建筑
mapview.showsBuildings = YES;
//显示用户的位置
mapview.showsUserLocation = YES;
//挂代理
mapview.delegate = self;
/*
让当前位置显示出来
1。采用定位管理器获取到定位权限
2。创建地图试图
3。设置追踪类型
4。让地图显示当前用户位置
5.设置显示区域
*/
//定位相关
manager = [[CLLocationManager alloc]init];
[manager requestAlwaysAuthorization];
/*
添加长按手势
此部分是为了长按地图出现大头针
*/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAn:)];
[mapview addGestureRecognizer:longPress];
}
//地图上用户位置刷新时调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
//改变用户大头针的数据
userLocation.title = @"宝宝是猪";
userLocation.subtitle = @"能吃能喝能睡能玩";
//设置地图的中心点
mapView.centerCoordinate = userLocation.coordinate;
//设置地图显示区域
//1.经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
//2.设置中心点
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
[mapView setRegion:MKCoordinateRegionMake(coor, span) animated:YES];
//反编码
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = placemarks.lastObject;
userLocation.title = placeMark.name;
userLocation.subtitle = placeMark.locality;
}];
}
//点击大头针触发
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"SSS");
}
/*
*1.大头针显示的原理
*2.往试图上添加数据模型
*3.调用代理方法将数据模型包装到大头针试图上面,然后返回该大头针视图
*/
//将大头针数据模型添加到视图上面时调用
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//判断当前大头针数据模型是不是用户位置的数据模型
if ([annotation isKindOfClass:[MKUserLocation class]]) {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationView"];
if (annotationView == nil) {
//将数据模型包装到大头针试图上面
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annotationView"];
}
annotationView.image = [UIImage imageNamed:@"user"];
//显示辅助视图
annotationView.canShowCallout = YES;
// 大头针拖动效果 长按
annotationView.draggable = YES;
//辅助标题左视图
annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"vip"]];
//辅助标题右视图
annotationView.rightCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"电话@3x"]];
//设置内容里面的辅助视图 无法与内容同时显示
// annotationView.detailCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"电话@3x"]];
return annotationView;
}
return nil;
}
//长按手势添加大头针
- (void)longAn:(UILongPressGestureRecognizer *)longGesture{
//如果长按手势是开始点击状态的话再添加大头针 防止重复调用生成多个大头针
if (longGesture.state != UIGestureRecognizerStateBegan) {
return;
}
//获取地图上的点
CGPoint point = [longGesture locationInView:longGesture.view];
//把点击地图上的点转化为经纬度
CLLocationCoordinate2D coordinate = [mapview convertPoint:point toCoordinateFromView:longGesture.view];
//反编码 将经纬度变为中文数据
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = placemarks.lastObject;
AnnotationModel *model = [[AnnotationModel alloc]init];
model.coordinate = coordinate;
model.title = placeMark.name;
//往地图上添加大头针 图片默认的
[mapview addAnnotation:model];
}];
});
}