iOS 第三方 map 地图

第一步 设置根控制器 添加导航栏信息 添加表格 设置代理 数据源

第二步 添加依赖库

第三步

@interface ViewController ()

{

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{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];if (!cell) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];}cell.textLabel.text = _provinceArr[indexPath.row];return cell;

}

第四步

创建跳转控制器 里面设置属性

@property (nonatomic , copy)NSString *passProvince;//传递生辉的字符串

第五步 属性传值 跳转

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//得到选中行对应的文本NSString *pro = _provinceArr[indexPath.row];//实例化地图控制器mapViewController *mapa = [[mapViewController alloc]init];mapa.passProvince = pro;[self.navigationController pushViewController:mapa animated:YES];

}

第六步

#import

#import

@interface mapViewController ()

@property (nonatomic , strong)MKMapView *mapView;

//地理位置管理者

@property (nonatomic ,strong)CLLocationManager *locManger;

@end

@implementation mapViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    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)currentBtnDidPress:(id)sender{

//开始获取位置信息 调用此方法后 协议中的方法才会执[self.locManger startUpdatingLocation];

}

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

  • (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)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 * _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;

}

设置 info.plist 里面 添加

在这里插入图片描述
授权


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部