第一步 设置根控制器 添加导航栏信息 添加表格 设置代理 数据源
第二步 添加依赖库
第三步
@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{
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 <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.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<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<MKAnnotation>)annotation{
static NSString *str = @"pin";
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:str];
//掉落效果
pin.animatesDrop = YES;
//泡泡效果
pin.canShowCallout = YES;
return pin;
}
设置 info.plist 里面 添加