地图

作者: 瓶邪_0bbc | 来源:发表于2019-01-06 20:39 被阅读0次

    Info.plit

    添加:Privacy - Location When In Use Usage Description

    Privacy - Location Always Usage Description

    Privacy - Location Always and When In Use Usage Description

    #import

    @interface MapUIkit : UIViewController

    //创建属性

    @property(nonatomic,strong)NSString *provie;

    @end

    #import "MapUIkit.h"

    //导入系统类

    #import

    //导入获取经纬度类

    #import

    //设置协议

    @interface MapUIkit ()<MKMapViewDelegate,CLLocationManagerDelegate>

    //创建地图对象

    @property(nonatomic,strong)MKMapView *MapView;

    @property(nonatomic,strong)CLLocationManager *lomanger;

    @end

    @implementation MapUIkit

    - (void)viewDidLoad {

        [super viewDidLoad];

        //设置导航标题

        self.navigationItem.title =self.provie;

        //设置右侧按钮

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"省会定位" style:UIBarButtonItemStyleDone target:self action:@selector(YOU:)];

        //实例化

        self.MapView = [[MKMapView alloc] initWithFrame:self.view.frame];

        //设置代理

        self.MapView.delegate = self;

        //设置地图样式

        self.MapView.mapType = MKMapTypeStandard;

        //加载视图

        [self.view addSubview:self.MapView];

        //设置按钮

        UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];

        //设置

        butt.frame=CGRectMake(40,600,50,50);

        //添加文字

        [buttsetTitle:@"当前位置" forState:UIControlStateNormal];

        butt.titleLabel.font = [UIFont systemFontOfSize:10];

        //添加点击事件

        [buttaddTarget:self action:@selector(dian:) forControlEvents:UIControlEventTouchUpInside];

        butt.layer.masksToBounds = YES;

        butt.layer.cornerRadius = 25;

        butt.backgroundColor = [UIColor greenColor];

        //添加视图

        [self.view addSubview:butt];

        //实例化经纬度类

        self.lomanger = [[CLLocationManager alloc] init];

        //申请用户授权用户进入后台不在授权

        [self.lomanger requestWhenInUseAuthorization];

    }

    - (void)YOU:(id)seb{

        //

        CLGeocoder *g = [[CLGeocoder alloc] init];

        //将地址字符串转换为位置的经纬度

        [ggeocodeAddressString:self.proviecompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

            //获取位置随便展示在地图上

            CLPlacemark*place = [placemarkslastObject];

            //获取位置

            CLLocation*loc =place.location;

            CLLocationCoordinate2D coor = loc.coordinate;

            //定位

            MKPointAnnotation *anne = [[MKPointAnnotation alloc] init];

            //设置挫钉

            anne.coordinate= coor;

            //回到主线程

            dispatch_async(dispatch_get_main_queue(), ^{

                //设置让地图显示区域缩小

                MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(coor, 10, 10);

                //添加到视图

                [self.MapViewsetRegion:rgin];

                [self.MapViewaddAnnotation:anne];

            });

        }];

    }

    //实现点击方法

    - (void)dian:(id)sender{

        //设置代理

        self.lomanger.delegate = self;

        //开始获取位置信息 调用此方法后协议中的方法才会执行

        [self.lomanger startUpdatingLocation];

    }

    //实现代理方法

    - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations{

        //创建对象获取当前获取最后一个元素

        CLLocation*curloc = [locationslastObject];

        //创建结构体 获取经纬度

        CLLocationCoordinate2DcurCoor = curloc.coordinate;

        self.lomanger.delegate = nil;

        //输出经纬度

        NSLog(@"经度%g,纬度%g",curCoor.longitude,curCoor.latitude);

        //设置让地图显示区域缩小

        MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(curCoor, 30, 30);

        //设置动画并添加

        [self.MapView setRegion:rgin animated:YES];

        //将地址经纬度转换为字符串

        CLGeocoder*Geder = [[CLGeocoderalloc]init];

        //设置方法

        [GederreverseGeocodeLocation:curloccompletionHandler:^(NSArray *_Nullableplacemarks,NSError*_Nullableerror) {

            //创建异步队列回到主线程

            dispatch_async(dispatch_get_main_queue(), ^{

                //获取最后一个经纬度转换为字符串

                CLPlacemark*place = [placemarksfirstObject];

                //设置大头针

                MKPointAnnotation *pino = [[MKPointAnnotation alloc] init];

                //将获取的地址名字给大头针

                pino.title= place.name;

                //设置大头针的位置

                pino.coordinate= curCoor;

                //添加到地图上

                [self.MapViewaddAnnotation:pino];

            });

        }];

    }

    //实现大头针点击事件

    - (nullableMKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id)annotation{

        //从MAPView找一块可用的内存

        MKPinAnnotationView *kl =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"1"];

        //设置动画

        kl.animatesDrop = YES;

        //设置

        kl.canShowCallout = YES;

        //返回内容

        returnkl;

    }

    @end

    作者:Bonpapier

    链接:https://www.jianshu.com/p/64d14b8b2976

    來源:简书

    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    相关文章

      网友评论

          本文标题:地图

          本文链接:https://www.haomeiwen.com/subject/eelerqtx.html