美文网首页
Mapkit地图LJY_Map

Mapkit地图LJY_Map

作者: Bonpapier | 来源:发表于2019-01-06 20:34 被阅读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

相关文章

  • Mapkit地图LJY_Map

    Info.plit 添加:Privacy - Location When In Use Usage Descrip...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

  • MapKit框架学习

    MapKit是用来显示地图的. MapKit的使用步骤导入框架MapKit导入头文件#import 在story...

  • 地图的简单使用(一)

    显示效果图 在使用地图的时候应先导入#import MKMapView是地图视...

  • iOS基础-系统地图使用

    地图开发注意: 项目中必须添加MapKit/MapKit框架 地图导航注意: 设置起点和终点 设置导航参数 通过M...

  • MapKit

    一、了解 MapKit框架提供地图的显示、导航、地点标注、增加图层等。利用MapKit可以集成一个全功能地图到ap...

  • iOS开发-地图开发(CoreLocation和MapKit))

    地图(CoreLocation和MapKit) CoreLocation框架CoreLocationCLLocat...

  • 地图自定义锚点+覆盖物

    导入系统库 通过拖控件的方式: 代码实现 导入头文件 #import< MapKit/MapKit.h>//地图#...

  • 苹果地图定位MapKit

    MapKit.framework拖入地图控件 设置NSLocationWhenInUseUsageDescript...

  • 地图(二)之MapKit

    MapKit 用于地图展示,例如大头针,路线,覆盖层展示等(着重界面展示) 一、MKMapView MapKit通...

网友评论

      本文标题:Mapkit地图LJY_Map

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