第一步 設(shè)置根控制器 ?添加導航欄信息 添加表格 ?設(shè)置代理 數(shù)據(jù)源
第二步 添加依賴庫
第三步?
@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;
}
第四步
創(chuàng)建跳轉(zhuǎn)控制器 ?里面設(shè)置屬性
@property (nonatomic , copy)NSString *passProvince;//傳遞生輝的字符串
第五步 屬性傳值 跳轉(zhuǎn)
-(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];
? ? //衛(wèi)星+平面的混合模式
? ? self.mapView.mapType = MKMapTypeHybrid;
? ? self.mapView.mapType = MKMapTypeStandard;
? ? [self.view addSubview:self.mapView];
? ? self.mapView.delegate = self;
? ? //獲取當前位置按鈕的創(chuàng)建
? ? 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; //設(shè)置代理
? ? //申請用戶授權(quán)
? ? [self.locManger requestAlwaysAuthorization];
}
-(void)currentBtnDidPress:(id)sender{
? ? //開始獲取位置信息 調(diào)用此方法后 協(xié)議中的方法才會執(zhí)
? ? [self.locManger startUpdatingLocation];
}
//獲取到位置信息后觸發(fā)的回調(diào)方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
? ? //獲取當前位置
? ? CLLocation *curLoc = [locations lastObject];
? ? //獲取經(jīng)緯度
? ? CLLocationCoordinate2D curCoor = curLoc.coordinate;
? ? NSLog(@"經(jīng)度:%g,緯度:%g",curCoor.longitude,curCoor.latitude);
? ? //地圖顯示區(qū)域縮小
? ? 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];
? ? }];
}
//用于設(shè)置錨點視圖的回調(diào)方法
-(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;
}
設(shè)置 info.plist 里面 添加