IOS 关于内置高德地图的使用(三)

#import "AnnotationViewController.h"

#import <MapKit/MapKit.h>

#import "CustomAnnotation.h"


@interface AnnotationViewController () <MKMapViewDelegate>


@property (strong, nonatomic) IBOutlet MKMapView *mapView;


- (IBAction)addAnnotation:(UIButton *)sender;


@end


@implementation AnnotationViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // 1. 跟踪用户位置

    self.mapView.userTrackingMode = MKUserTrackingModeFollow;

    

    // 2. 设置地图类型

    self.mapView.mapType = MKMapTypeStandard;

    

    // 3. 设置代理

    self.mapView.delegate = self;

    

#pragma mark----添加手势, mapView 添加一个轻拍手势

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMapView:)];


    [self.mapView addGestureRecognizer:tapGR];

}


// 监听 mapView 的轻拍事件

- (void)tapMapView:(UIGestureRecognizer *)tap

{

    // 1. 获取用户点击在 mapView 上的位置: x, y

   CGPoint point = [tap locationInView:self.mapView];

    // 2. 将数学坐标系中的点转换成经纬度坐标

    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

    

    // 3. 创建大头针模型对象

    CustomAnnotation *customAnno = [[CustomAnnotation alloc]init];

    

#pragma mark---- 必须要实现的属性

        // 必须要设置的属性

    customAnno.coordinate = coordinate;

    customAnno.title = @"hahahahahah";

    customAnno.imageName = @"2";

    // 4. 将大头针添加到 mapView

    [self.mapView addAnnotation:customAnno];

    

}







#pragma mark----实现 MKMapViewDelegate 代理方法

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    

}


// 如果想自定义大头针, 需要实现协议中的一个方法

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

{

    // 如果不是自定义的大头针, 则返回

    if (![annotation isKindOfClass:[CustomAnnotation class]]) {

        return nil;

    }

    // 先到重用集合中去取 大头针视图对象 (MKAnnotationView)

    static NSString *identifier = @"custon";

    MKAnnotationView *annotationViewCell = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationViewCell == nil) {

        annotationViewCell = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];

        

        // 显示子标题 title subtitle

        annotationViewCell.canShowCallout = YES;

        // 设置大头针上显示的描述区域的偏移量

        annotationViewCell.calloutOffset = CGPointMake(0, -10); // - 向上, + 向下

        // 设置大头针上面显示的描述区域 左右两边的辅助空间

        annotationViewCell.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

        annotationViewCell.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        

    }

    

    

    // 将传过来的 annotation annotationView赋值(传递模型对象)

    annotationViewCell.annotation = annotation;

    // 设置图片

    CustomAnnotation *customAnno = (CustomAnnotation *)annotation;

    annotationViewCell.image = [UIImage imageNamed:customAnno.imageName];

    

    return annotationViewCell;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值