原生地图

第一步 导入框架

#import <MapKit/MapKit.h>

第二步,如果用到定制大头针落下动画,和复用方法则需要代理
@interface RootViewController ()<MKMapViewDelegate>

第三步正文

- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_mapView];
    //设置具体显示位置(显示坐标用到CLLocationCoordinate2D)
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.896304, 116.410103);
    //设置放大比例 (数字越小放大比例越大,记住最后一个就行了)
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(coord, span);
    [_mapView setRegion:region];
    _mapView.delegate = self;
    //添加长按手势
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick:)];
    [_mapView addGestureRecognizer:longPress];
    
}
-(void)longPressClick:(UILongPressGestureRecognizer *)longPress
{
   
    /*
     此状态长按会出现无数个大头针
    //添加大头针
    MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init];
    annotation.title = @"主标题";
    annotation.subtitle = @"副标题";
    //设置位置
    //获取中心点
    CGPoint point = [longPress locationInView:_mapView];
    //折算经纬度
    CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:_mapView];
    annotation.coordinate = coord;
    [_mapView addAnnotation:annotation];
    
   
    */
     
     //修改后的代码
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init];
        annotation.title = @"主标题";
        annotation.subtitle = @"副标题";
        //设置位置
        //获取中心点
        CGPoint point = [longPress locationInView:_mapView];
        //折算经纬度
        CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:_mapView];
        annotation.coordinate = coord;
        [_mapView addAnnotation:annotation];
    }
    
    
}

#pragma mark 地图的大头针代理方法
//使用原生大头针
/*
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView * pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
    if (!pin) {
        pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
    }
    //设置大头针颜色
    pin.pinColor = arc4random()%3;
    //掉下来的动画
    pin.animatesDrop = YES;
    
    //如果实现了代理,气泡默认是不弹出来的,需要设置
    pin.canShowCallout = YES;
    //需要重新设置内容
    [pin setAnnotation:annotation];
    //添加左边头像
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    image.image = [UIImage imageNamed:@"header_default.jpg"];
    pin.leftCalloutAccessoryView = image;
    
    //添加button
    UIButton * button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 20);
    pin.rightCalloutAccessoryView =button;
    return pin;
}
 */
//自定义大头针
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView * view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
    if (!view) {
        view = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
    }
    //没有设置颜色,没有设置掉下来的动画
    //自己设置图片,照片为自己设置照片的初始大小,可以自己设定
    view.image = [UIImage imageNamed:@"ic_hole_border"];
    //设置气泡弹出
    view.canShowCallout = YES;
    UIImageView * leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImage.image = [UIImage imageNamed:@"header_default.jpg"];
    view.leftCalloutAccessoryView = leftImage;
    //设置动画效果
    view.frame = CGRectMake(-100, -100, 35, 35);
    [UIView animateWithDuration:0.4 animations:^{
        view.frame = CGRectMake(0, 0, 30, 30);
    }];
    return view;
}
 
原文地址:https://www.cnblogs.com/huoxingdeguoguo/p/4676205.html