MAP 地图

MAP 地图

  1. Main.storyboard 中设置控制器 导航 表格
  2. 导入第三方 框架
1CoreLocation.framework
2MapKit.framework
  1. viewcontrol
在这里插入代码片//
//  ViewController.m
//  lyMap
//
//  Created by 毅先森 on 2018/10/11.
//  Copyright © 2018 刘毅. All rights reserved.
//

#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

4map viewcontrol.h 定义 属性 接受数据 (copy)nsstring *.passProvince;
5 map viewcontrol

//
//  OneViewController.m
//  lyMap
//
//  Created by 毅先森 on 2018/10/11.
//  Copyright © 2018 刘毅. All rights reserved.
//

#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];
    //初始化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.locManager = [[CLLocationManager alloc]init];
    //签订代理
    self.locManager.delegate = self;
    
    //申请用户授权
    [self.locManager requestWhenInUseAuthorization];
    
}
//获取当前按钮触发
-(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) {
        
        //获取其中的一个地址信息   j街道具体位置
        CLPlacemark *place = [placemarks lastObject];
        
        //做一个大头针
        //初始化一个描点
        MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
        //设置大头针的标题   显示当前的位置信息
        point.title = 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

6 在Info.plist 添加

1 Privacy - Location Always and When In Use Usage Description
2 Privacy - Location Always Usage Description
3 Privacy - Location When In Use Usage Description
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值