在.h中加入属性
//创建属性
@property(nonatomic,strong)NSString *provie;
导入头文件
//导入系统类
#import <MapKit/MapKit.h>
//导入获取经纬度类
#import <CoreLocation/CoreLocation.h>
//设置协议
@interface MapUIkit ()
<MKMapViewDelegate,CLLocationManagerDelegate>
//创建地图对象
@property(nonatomic,strong)MKMapView *MapView;
@property(nonatomic,strong)CLLocationManager *lomanger;
@end
@implementation MapUIkit
-
(void)viewDidLoad {
[super viewDidLoad];
//设置导航标题
self.navigationItem.title =self.provie;
//设置右侧按钮
//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@“省会定位” style:UIBarButtonItemStyleDone target:self action:@selector(YOU:)];
//实例化
self.MapView = [[MKMapView alloc] initWithFrame:self.view.frame];
//设置代理
self.MapView.delegate = self;
//设置地图样式
self.MapView.mapType = MKMapTypeStandard;
//加载视图
[self.view addSubview:self.MapView];
//设置按钮
UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
//设置
butt.frame = CGRectMake(40, 600,50, 50);
//添加文字
[butt setTitle:@“当前位置” forState:UIControlStateNormal];
butt.titleLabel.font = [UIFont systemFontOfSize:10];
//添加点击事件
[butt addTarget:self action:@selector(dian:) forControlEvents:UIControlEventTouchUpInside];
butt.layer.masksToBounds = YES;
butt.layer.cornerRadius = 25;
butt.backgroundColor = [UIColor greenColor];
//添加视图
[self.view addSubview:butt];
//实例化经纬度类
self.lomanger = [[CLLocationManager alloc] init];
//申请用户授权用户进入后台不在授权
[self.lomanger requestWhenInUseAuthorization];
} -
(void)YOU:(id)seb{
//
CLGeocoder *g = [[CLGeocoder alloc] init];
//将地址字符串转换为位置的经纬度
[g geocodeAddressString:self.provie completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取位置随便展示在地图上
CLPlacemark *place = [placemarks lastObject];
//获取位置
CLLocation *loc =place.location;
CLLocationCoordinate2D coor = loc.coordinate;
//定位
MKPointAnnotation *anne = [[MKPointAnnotation alloc] init];
//设置挫钉
anne.coordinate = coor;
//回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//设置让地图显示区域缩小
MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(coor, 10, 10);
//添加到视图
[self.MapView setRegion:rgin];
[self.MapView addAnnotation:anne];
});
}];
}
//实现点击方法 -
(void)dian:(id)sender{
//设置代理
self.lomanger.delegate = self;
//开始获取位置信息 调用此方法后协议中的方法才会执行[self.lomanger startUpdatingLocation];
}
//实现代理方法 -
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//创建对象获取当前获取最后一个元素
CLLocation *curloc = [locations lastObject];
//创建结构体 获取经纬度
CLLocationCoordinate2D curCoor = curloc.coordinate;
self.lomanger.delegate = nil;
//输出经纬度
NSLog(@“经度%g,纬度%g”,curCoor.longitude,curCoor.latitude);//设置让地图显示区域缩小
MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(curCoor, 30, 30);
//设置动画并添加
[self.MapView setRegion:rgin animated:YES];
//将地址经纬度转换为字符串
CLGeocoder *Geder = [[CLGeocoder alloc] init];
//设置方法
[Geder reverseGeocodeLocation:curloc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//创建异步队列回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//获取最后一个经纬度转换为字符串
CLPlacemark *place = [placemarks firstObject];
//设置大头针
MKPointAnnotation *pino = [[MKPointAnnotation alloc] init];
//将获取的地址名字给大头针
pino.title = place.name;
//设置大头针的位置
pino.coordinate = curCoor;
//添加到地图上
[self.MapView addAnnotation:pino];
});
}];
}
//实现大头针点击事件 -
(nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
//从MAPView找一块可用的内存
MKPinAnnotationView *kl =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@“1”];
//设置动画
kl.animatesDrop = YES;
//设置
kl.canShowCallout = YES;
//返回内容
return kl;
}
@end