ios第三方map

iOS 第三方map

  • 创建根控制器 添加导航 添加表格 因为省事直接拖拽 记得签订代理协议
  • 导入第三方map所需要的依赖库
    在这里插入图片描述
  • ViewController.m 代码
#import "ViewController.h"
//引入头文件
#import "OneViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>//签订协议
{
    //定义省份数组
    NSArray *_provinceArr;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //给省份数组初始化赋值
    _provinceArr = @[@"石家庄",@"廊坊",@"北京",@"保定",@"天津",@"衡水",@"张家口",@"邯郸",@"雄安",@"安阳",@"洛阳"];
}
//表格  ‘行’
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回数组的个数
    return _provinceArr.count;
}
//表格  ‘单元格’
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //静态字符串标识
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    //给cell赋值
    cell.textLabel.text = _provinceArr[indexPath.row];
    return cell;
}
//点击cell进行跳转进第二个界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //得到点击相对应的cell里面的文本
    NSString *str = _provinceArr[indexPath.row];
    //初始化第二个控制器
    OneViewController *one = [[OneViewController alloc]init];
    //给第二个界面传参  '把得到点击的cell的文本传到one控制器的字符串passProvince里面'
    one.passProvince = str;
    //跳转到第二个界面
    [self.navigationController pushViewController:one animated:YES];
}


@end

  • OneViewController.h代码
#import "ViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface OneViewController : ViewController
//定义传参的参数  ‘传第一个界面省会数组的数据’
@property(nonatomic,copy)NSString *passProvince;
@end

NS_ASSUME_NONNULL_END
  • OneViewController.m代码
#import "OneViewController.h"
//引入头文件 '地图框架'
#import <MapKit/MapKit.h>
//引入头文件 ‘定位框架’
#import <CoreLocation/CoreLocation.h>

@interface OneViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>//地图视图协议
//定义属性 ‘地图视图’
@property(nonatomic,strong)MKMapView *MapView;
//定义属性 ‘地理位置管理者’
@property(nonatomic,strong)CLLocationManager *locManager;



@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航标题  显示当前省会
    self.navigationItem.title = self.passProvince;
    //初始化MKMapView ‘地图视图’
    self.MapView = [[MKMapView alloc]initWithFrame:self.view.frame];
    //设置地图的样式  'MKMapTypeSatellite卫星地图' ‘MKMapTypeStandard平面地图’
    self.MapView.mapType = MKMapTypeStandard;
    //签订协议
    self.MapView.delegate = self;
    //添加到视图
    [self.view addSubview:self.MapView];
    
    
    //设置按钮样式 ‘UIButtonTypeRoundedRect圆角’
    UIButton *Btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //获取当前按钮的位置
    Btn.frame = CGRectMake(10, self.view.frame.size.height-80, 40, 40);
    //设置按钮的文字
    [Btn setTitle:@"定位" forState:UIControlStateNormal];
    //设置按钮的颜色
    Btn.backgroundColor = [UIColor cyanColor];
    //给按钮添加方法
    [Btn addTarget:self action:@selector(currentBtnDidPress:) forControlEvents:UIControlEventTouchUpInside];
    //添加到视图上去
    [self.view addSubview:Btn];
    
    //添加右侧导航条按钮获取省会位置信息   定位省会位置
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"省会定位" style:UIBarButtonItemStylePlain target:self action:@selector(getProvinceLocation:)];
    
    //初始化 地理位置管理者对象
    self.locManager = [[CLLocationManager alloc]init];
    //签订代理
    self.locManager.delegate = self;
    
    //申请用户授权
    [self.locManager requestWhenInUseAuthorization];
    
}
//获取省会位置
-(void)getProvinceLocation:(id)sender{
    CLGeocoder *coder = [[CLGeocoder alloc]init];
    //做地址解析,讲地址字符串转换为地址经纬度
    [coder geocodeAddressString:self.passProvince completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //随便获取一个地址信息展示在地图上
        CLPlacemark *place = [placemarks lastObject];
        //获取位置   获取place地址信息中最后一个地址信息
        CLLocation *location = place.location;
        //获取经纬度   获取location的经纬度
        CLLocationCoordinate2D Coor = location.coordinate;
        //定义锚点 添加到地图上去
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
        //设置锚点经纬度
        annotation.coordinate = Coor;
        //回到UI主线程添加锚点
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //让地图的显示区缩小   MKCoordinateRegion显示区    传入一个经纬度 和两个需要缩成的大小
            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(Coor, 180, 180);
            //添加到地图视图上  给地图缩小
            [self.MapView setRegion:region animated:YES];
            
            
            
            //添加到地图上去
            [self.MapView addAnnotation:annotation];
        });
    }];
}


//获取当前按钮触发
-(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<MKAnnotation>)annotation{
    //静态字符串标识
    static NSString *iden = @"pin";
    //初始化描点视图
    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:iden];
    
    //设置掉落状态
    pin.animatesDrop = YES;
    //设置泡泡效果
    pin.canShowCallout = YES;
    
    return pin;
}



@end

  • Info.plist文件或许地理位置授权
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wangai9140/article/details/83021676