iOS 第三方 map 地图

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

第二步 添加依赖库

依赖库

第三步 

@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 里面 添加

授权
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,691评论 3 3
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,090评论 3 38
  • App.m ViewController *vc = [[ViewController alloc]init]; ...
    本泽马阅读 352评论 0 0
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,142评论 1 32
  • 肥月送我进候车室,工作人员不让,她念叨了两遍-以前不是可以么。无法,她只好走了,又回头看看我。趁检验箱子的间隙抬头...
    大笑沧海阅读 226评论 0 1