@import CoreLocation;//经纬度
@import MapKit;//地图库
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property(nonatomic,strong) CLLocationManager *locationManage;
@property(nonatomic,strong) MKMapView *mapView;
//加载UI
- (void)setupUI{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"详情" style:UIBarButtonItemStylePlain target:self action:@selector(details)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"定位" style:UIBarButtonItemStylePlain target:self action:@selector(positioning)];
//地图类型切换
UISegmentedControl *mapTypeSegmentControl = [[UISegmentedControl alloc] initWithItems:@[@"普通", @"卫星", @"混合"]];
mapTypeSegmentControl.bounds = CGRectMake(0, 0, 200, 25);
mapTypeSegmentControl.selectedSegmentIndex = 0;
[mapTypeSegmentControl addTarget:self action:@selector(progressSEgmentControl:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:mapTypeSegmentControl];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
self.toolbarItems = @[flexibleItem, segmentItem, flexibleItem];
//实例化地图
_mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
//是否显示用户标注
_mapView.showsUserLocation = YES;
/*
mapType:设置地图类型
MKMapTypeStandard:标准地图,一般情况下使用此地图即可满足
MKMapTypeSatellite:卫星地图
MKMapTypeHybrid:混合地图,加载最慢比较消耗资源
*/
_mapView.mapType = MKMapTypeStandard;//标准地图
//设置代理
_mapView.delegate = self;
[self.view addSubview: self.mapView];
//添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressMapView:)];
longPress.minimumPressDuration = 1;
[self.view addGestureRecognizer:longPress];
}
//定位
- (void)positioning{
[_locationManage startUpdatingLocation];
}
//切换地图
- (void)progressSEgmentControl:(UISegmentedControl *)sender{
_mapView.mapType = sender.selectedSegmentIndex;
}
//启动定位服务
- (void)loadLocationService{
//先配置info.plist 关键字 NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription 配置设置选项
/**
NSLocationAlwaysUsageDescription 后台一直开启定位服务
NSLocationWhenInUseUsageDescription 只在程序运行时开启定位服务
*/
//实例化位置管理器
_locationManage = [[CLLocationManager alloc]init];
//判断异常情况
if (![CLLocationManager locationServicesEnabled]) {
//弹出警告框
NSLog(@"定位服务不可用");
}
//弹出权限提示框:请求定位授权
[_locationManage requestWhenInUseAuthorization];
_locationManage.delegate = self;
//定位频率和定位精度并不应当越精确越好,需要视实际情况而定,因为越精确越耗性能,也就越费电。
//定位精度
_locationManage.desiredAccuracy = kCLLocationAccuracyBest;//最精确
//位置更新,最小距离:定位频率
_locationManage.distanceFilter = kCLDistanceFilterNone;//动一下,更新一下
//启动定位
// 注意:开始定位比较耗电,不需要定位的时候最好调用[stopUpdatingLocation]结束定位。
[_locationManage startUpdatingLocation];
}
//调用位置,查看位置更新是否成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"位置信息:%@",manager.location);
//停止定位
[manager stopUpdatingLocation];
[self updateMapViewWithLocation:manager.location];
}
//更加新地图
- (void)updateMapViewWithLocation:(CLLocation *)location{
//跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);
//范围
MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);
//配置地图显示区域
[_mapView setRegion:region animated:YES];
//配置当前位置的信息
_mapView.userLocation.title = @"当前位置";
_mapView.userLocation.subtitle = [NSString stringWithFormat:@"%0.2f,%0.2f",location.coordinate.latitude,location.coordinate.longitude];//调经度和纬度
}
- (void)longPressMapView:(UILongPressGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateBegan) {
//获取手势坐标
CGPoint piont = [gesture locationInView:gesture.view];
//视图坐标转化为经纬度坐标
CLLocationCoordinate2D coordiante = [_mapView convertPoint:piont toCoordinateFromView:gesture.view ];
//利用经纬度获取CLLocation
CLLocation *location = [[CLLocation alloc]initWithLatitude:coordiante.latitude longitude:coordiante.longitude];
//地理位置逆编码
[self veverseGeoLocation:location completion:^(BOOL sucess, id content) {
if (sucess) {
NSLog(@"%@",content);
//大头针
MKPointAnnotation *annotaTion = [[MKPointAnnotation alloc]init];
annotaTion.coordinate =coordiante;//获得经纬度
annotaTion.title = content[@"Name"];//标题
[_mapView addAnnotation:annotaTion];
}
}];
}
}
//地理位置逆编码:把经纬度信息编码成格式化的地理位置信息
- (void)veverseGeoLocation:(CLLocation *)location completion:(void(^)(BOOL sucess,id content))completion{
//逆编码
CLGeocoder *coder = [[CLGeocoder alloc]init];
//逆编码方法,后台线程进行
[coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
BOOL sucess;
id content = nil;
if (error) {
sucess = NO;
content = error.localizedDescription;
}else{
sucess = YES;
CLPlacemark *placemark = placemarks.firstObject;
content = placemark.addressDictionary;
}
//执行 block
completion(sucess,content);
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
//定义标识符
static NSString *viewIdentifer = @"AnnotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifer];
if (!annotationView) {
annotationView =[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:viewIdentifer];
//颜色
annotationView.tintColor = [UIColor redColor];
//是否动画
annotationView.animatesDrop = YES;
//是否需要点击
annotationView.canShowCallout = YES;
//配置详细按钮
UIButton *detaiButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
annotationView.rightCalloutAccessoryView = detaiButton;
}
return annotationView;
}
//点击弹出框
- (void)mapVIEW:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryContrelTapped:(UIControl *)control{
NSLog(@"当前位置");
}