封装好的地图

在info中添加
Privacy - Location Always and When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description

添加MapKit.framework,CoreLocation.framework的库

在.h中

@property (nonatomic , copy)NSString *passProvince;//传递生辉的字符串
在.m中
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface mapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic , strong)MKMapView *mapView;
//地理位置管理者
@property (nonatomic ,strong)CLLocationManager *locManger;

@end

@implementation mapViewController

-(void)viewDidLoad {
[super viewDidLoad];
self .navigationItem.title = self.passProvince;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@“省会信息” style:UIBarButtonItemStylePlain target:self action:@selector(asd:)];
self.mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
//卫星+平面的混合模式
self.mapView.mapType = MKMapTypeHybrid;
self.mapView.mapType = MKMapTypeStandard;
[self.view addSubview:self.mapView];
self.mapView.delegate = self;
//获取当前位置按钮的创建

UIButton *currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];

currentBtn.frame = CGRectMake(10, self.view.frame.size.height - 80, 40, 40);

[currentBtn setTitle:@"定位" forState:UIControlStateNormal];

currentBtn.backgroundColor = [UIColor blueColor];

[currentBtn addTarget:self action:@selector(currentBtnDidPress:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:currentBtn];

//实例化位置管理利器对象
self.locManger = [[CLLocationManager alloc]init];

self.locManger.delegate = self; //设置代理

//申请用户授权
[self.locManger requestAlwaysAuthorization];

}
//获取省会位置
-(void)asd:(id)sender{
CLGeocoder * q = [[CLGeocoder alloc]init];
//坐地址解析 将字符串转换为经纬度
[q geocodeAddressString:self.passProvince completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取一个位置 展示在地图上
CLPlacemark * place = [placemarks lastObject];
//获取位置
CLLocation * loc = place.location;
//获取经度
CLLocationCoordinate2D cor = loc.coordinate;
//定义锚点
MKPointAnnotation * an = [[MKPointAnnotation alloc]init];
an.coordinate =cor;
dispatch_async(dispatch_get_main_queue(), ^{
//显示区域
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(cor, 800, 800);

        [self.mapView setRegion:region animated:YES];
        [self.mapView addAnnotation:an];
    });
}];

}
-(void)currentBtnDidPress:(id)sender{
//开始获取位置信息 调用此方法后 协议中的方法才会执
[self.locManger startUpdatingLocation];

}
//获取到位置信息后触发的回调方法

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取当前位置
    CLLocation *curLoc = [locations lastObject];
    //获取经纬度
    CLLocationCoordinate2D curCoor = curLoc.coordinate;
    NSLog(@“经度:%g,纬度:%g”,curCoor.longitude,curCoor.latitude);

    //地图显示区域缩小
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(curCoor, 800, 800);

    [self.mapView setRegion:region animated:YES];

    //反向地址解析
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:curLoc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

      //获取其中一个地址信息
      CLPlacemark *plack = [placemarks lastObject];
      
      //做一个大头针 定在当前位置上 锚点
      MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
      
      point.title = plack.name;
      
      point.coordinate = curCoor;
      
      [self.mapView addAnnotation:point];
    

    }];

}
//用于设置锚点视图的回调方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

static NSString *str = @"pin";

MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:str];

//掉落效果
pin.animatesDrop = YES;

//泡泡效果
pin.canShowCallout = YES;

return pin;

}
@end

猜你喜欢

转载自blog.csdn.net/WNEHIUZHNAG/article/details/84930444
今日推荐