MKMapView (地图)

//使用地图

#import "ViewController.h"

#import <MapKit/MapKit.h> 

@interface ViewController ()<MKMapViewDelegate> 

@end 

@implementation ViewController 

- (void)viewDidLoad {    

    [super viewDidLoad];

    //1 创建 map 视图

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:mapView];

    

    //2 地图参数设置;

    [mapView setMapType:MKMapTypeStandard];//地图的显示模式

    [mapView setZoomEnabled:YES];//放大 默认是yes

    [mapView setScrollEnabled:YES];//滑动地图

    [mapView setRotateEnabled:YES];//旋转的操作

    [mapView setPitchEnabled:YES];

    

    //1 设置显示区域 121.318489,31.302382  ;0.1是初始的比例(跨度)

    MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(31.302382, 121.318489), MKCoordinateSpanMake(0.1, 0.1));

    

    //2 通过距离来设置 地图的显示区域

//    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(31.302382, 121.318489), 10, 10);

    //添加到地图上

    [mapView setRegion:[mapView regionThatFits:region]];

    

    //设置地图标注

    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(31.302382, 121.318489);

    CLLocationCoordinate2D coordinate2d = CLLocationCoordinate2DMake(31.302382, 120.318489);

    //大头针 一

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

    [pointAnntation1 setTitle:@"当前位置"];

    [pointAnntation1 setSubtitle:@"当前说明"];

    [pointAnntation1 setCoordinate:coordinate2D];

    

    //大头针  二

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

    [pointAnntation2 setTitle:@"当前位置"];

    [pointAnntation2 setSubtitle:@"当前说明"];

    [pointAnntation2 setCoordinate:coordinate2d];

    

//    NSArray *pointArray = @[pointAnntation1,pointAnntation2];

    //把位置的 红点 添加到地图

    [mapView addAnnotations:@[pointAnntation1,pointAnntation2]];

       

    // Do any additional setup after loading the view, typically from a nib.

//设置添加大头针

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 

    //设置大头针属性    

    //优化处理 如果设置两个 内存会有问题

    static NSString *pinIndentifier = @"pin";

    MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinIndentifier];

    if (!pinIndentifier) {

        pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIndentifier];

        //    [pinAnnotationView setPinColor:MKPinAnnotationColorPurple];//已经废弃

        //大头针动画的效果 下落的样式 出现

        [pinAnnotationView setAnimatesDrop:YES];//没有出现下落的效果

        //标题显示打开

        [pinAnnotationView setCanShowCallout:YES];

    }

    //这个初始化会有内存问题:

//    MKPinAnnotationView *pinAnnotationView = [[MKPinAnnotationView alloc] init];

//    [pinAnnotationView setPinColor:MKPinAnnotationColorPurple];//已经废弃

    //大头针动画的效果 下落的样式 出现

//    [pinAnnotationView setAnimatesDrop:YES];//没有出现下落的效果

    //标题显示打开

//    [pinAnnotationView setCanShowCallout:YES];

    return pinAnnotationView;

}

#pragma mark   mapViewDelegate

//添加标注时调用的方法

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(nonnull NSArray<MKAnnotationView *> *)views{

}

//标注被选中时 执行的方法

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(nonnull MKAnnotationView *)view{    

}

//标注失去焦点时执行的方法

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{

}

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView{

    //地图将要载入

}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{

    //地图载入完成以后执行的方法

}

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error{

    //地图载入失败的时候的执行的方法

    NSLog(@"error:%@",[error description]);

}

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

    //地图显示区域将要发生变化是执行的方法

}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    //地图显示区域 发生变化 执行的方法

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

@end

//注意:地图的使用需要引入框架,并且接受协议

原文地址:https://www.cnblogs.com/jiurong001/p/5205021.html