(笔记)在地图上添加大头针

(四)大头针的基本操作(Annotation)
  • 添加一个大头针
    • - (void)addAnnotation:(id<MKAnnotation>)annotation;
  • 移除一个大头针
    • - (void)removeAnnotation:(id<MKAnnotation>)annotation;

1.添加大头针的步骤
  • ①创建大头针模型类继承NSObject,并遵守<MKAnnotation>协议
  • ②定义其属性@coordinate(经纬度),@title(标题),@subTitle(子标题)
  • ③创建一个大头针模型的实例,为属性赋值。
  • [self.mapViewaddAnnotation:anno];
    • 添加大头针

2.通过点击屏幕,添加大头针

-( void )touchesBegan:( NSSet < UITouch *> *)touches withEvent:( UIEvent *)event{
   
// 获取点击在屏幕上的点
   
CGPoint point = [[touches anyObject ] locationInView : self . view ];
   
// 将点转换过成经纬度
   
CLLocationCoordinate2D touchCoodinate = [ self . mapView convertPoint :point toCoordinateFromView : self . view ];
   
// 创建大头针对象
   
DJAnnotation * anno = [ DJAnnotation new ];
    anno.
coordinate = touchCoodinate;
    anno.
title = @" 标题 " ;
    anno.
subtitle = @" 子标题 " ;
   
    [
self . mapView addAnnotation :anno];
   
}


(五)自定义大头针对象
1.自定义大头针

// 当向地图添加大头针,自动调用该方法
// 大头针是可以重用的。
-(
MKAnnotationView *)mapView:( MKMapView *)mapView viewForAnnotation:( id < MKAnnotation >)annotation{
   
// 如果传进来的是用户位置,直接返回 nil ,使用系统的大头针
   
if ([annotation isKindOfClass :[ MKUserLocation class ]]) return nil ;
   
   
static NSString * ID = @"annotation" ;
   
// 因为 MKAnnotationView 默认是不显示的,所以用其子类
   
MKPinAnnotationView * annoView = ( MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier :ID];
   
if (!annoView) {
        annoView = [[
MKPinAnnotationView alloc ] initWithAnnotation : nil reuseIdentifier :ID];
       
//1. 设置大头针的标题可以呼出
        annoView.
canShowCallout = YES ;
       
//2. 设置大头针的颜色
        annoView.
pinTintColor = [ UIColor yellowColor ];
       
//3. 设置大头针掉落效果
        annoView.
animatesDrop = YES ;
    }
    // 设置大头针的大头针模型
    annoView. annotation = annotation;
    return annoView;
}



2.自定义大头针的图片
  • ①自定义一个大头针类型的类,继承于 MKAnnotationView
  • ②在控制器中创建自定义大头针实例
  • ③设置大头针的图片。annoView.image = xxx;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值