1.创建导航 在导航上创建定位按钮
2.引入两个依赖库 一个是地图的 一个是定位的
在VC.m里面:
引入两个框架 地图和定位的框架
#import “ViewController.h”
//引入头文件 ‘地图框架’
#import <MapKit/MapKit.h>
//引入头文件 ‘定位框架’
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<MKMapViewDelegate , CLLocationManagerDelegate>
//定义属性 ‘地图视图’
@property(nonatomic,strong)MKMapView *MapView;
//定义属性 ‘地理位置管理者’
@property(nonatomic,strong)CLLocationManager *locManager;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @“地图”;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“附近违章高发地” style:UIBarButtonItemStyleDone target:self action:@selector(click)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“定位” style:UIBarButtonItemStyleDone target:self action:@selector(currentBtnDidPress:)];//初始化MKMapView ‘地图视图’
self.MapView = [[MKMapView alloc]initWithFrame:self.view.frame];
//设置地图的样式 ‘MKMapTypeSatellite卫星地图’ ‘MKMapTypeStandard平面地图’
self.MapView.mapType = MKMapTypeStandard;
//签订协议
self.MapView.delegate = self;
//添加到视图
[self.view addSubview:self.MapView];//实例化位置管理者对象
self.locManager = [[CLLocationManager alloc]init];
//签订协议
self.locManager.delegate = self;
//申请用户授权
[self.locManager requestWhenInUseAuthorization];
}
-(void)click{
// NextViewController *next = [[NextViewController alloc]init];
// [self.navigationController pushViewController:next animated:YES];
}
//获取当前按钮触发
-(void)currentBtnDidPress:(id)sender{
//开始获取位置信息,调用此方法后,协议中的方法才会执行
[self.locManager startUpdatingLocation];//通过位置管理者开始位置更新
}
//获取当前位置信息后触发的回调方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//获取当前位置 CLLocation位置 locations位置信息一个数组,获取最后一个最新的位置信息
CLLocation *cation = [locations lastObject];
//获取经纬度 coordinate坐标 ‘经纬度=当前位置的坐标’
CLLocationCoordinate2D locationCoor = cation.coordinate;
//输出一下位置信息 longitude经度 latitude纬度
NSLog(@"位置:经度:%g,纬度:%g",locationCoor.longitude,locationCoor.latitude);
//让地图的显示区缩小 MKCoordinateRegion显示区 传入一个经纬度 和两个需要缩成的大小
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(locationCoor, 180, 180);
//添加到地图视图上 给地图缩小
[self.MapView setRegion:region animated:YES];
//反向地址解析,将经纬度转换为字符串
//初始化地址解析 ‘CLGeocoder地址解析’
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
//解析 reverseGeocodeLocation传入一个位置
[geocoder reverseGeocodeLocation:cation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取其中的一个地址信息
CLPlacemark *place = [placemarks lastObject];
//做一个大头针
//初始化一个描点
MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
//设置大头针的标题 显示当前的位置信息
point.title = [NSString stringWithFormat:@"我的位置:%@",place.name];
//设置大头针显示的经纬度 传入locationCoor当前的经纬度
point.coordinate = locationCoor;
//添加到地图上 必须是Annotation的类型
[self.MapView addAnnotation:point];
}];
}
//设置锚点视图的回调方法,当地图调用addAnnotation:方法时会触发 点击大头针是触发的方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
//静态字符串标识
static NSString *iden = @“pin”;
//初始化描点视图
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:iden];
//设置掉落状态
pin.animatesDrop = YES;
//设置泡泡效果
pin.canShowCallout = YES;
return pin;
}
@end