自定义:地图标注(大头针)
来源:互联网 发布:架子鼓打谱软件 编辑:程序博客网 时间:2024/05/22 08:00
地图标注
所谓的地图标注就是我们俗称的大头针:
钉在某个具体位置,用来标识这个位置上有特定的事物(比如地图某位置上标注有家餐馆)
大头针的基本操作
添加单个大头针 - (void)addAnnotation:(id <MKAnnotation>)annotation;
添加多个大头针 - (void)addAnnotations:(NSArray *)annotations;
移除一个大头针 - (void)removeAnnotation:(id <MKAnnotation>)annotation;
移除多个大头针 - (void)removeAnnotations:(NSArray *)annotations;
(id <MKAnnotation>)annotation
参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据
大头针模型
新建一个大头针模型类
#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface YXYMKAnnotation : NSObject<MKAnnotation>// 遵守协议,自定义大头针数据模型/** 坐标位置 */@property (nonatomic, assign) CLLocationCoordinate2D coordinate;/** 标题 */@property (nonatomic, copy) NSString *title;/** 子标题 */@property (nonatomic, copy) NSString *subtitle;/** 图标字符串 */@property (nonatomic, copy) NSString *icon;@end
自定义大头针
如何自定义大头针:
1.设置MKMapView的代理
2.实现下面的代理方法,返回大头针控件 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
3.根据传进来的(id )annotation参数创建并返回对应的大头针控件
代理方法的使用注意!
如果返回nil,显示出来的大头针就采取系统的默认样式
标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法
因此,需要在代理方法中分清楚(id )annotation参数代表自定义的大头针还是蓝色发光圆点
#pragma mark - MKMapViewDelegate 封装过的代码- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ // 判断如果是定位用户的大头针,用系统自己的大头针样式,而不是用自定义的,防止崩溃 // 返回nil就会按照系统的默认方法创建 if (![annotation isKindOfClass:[YXYMKAnnotation class]]){ return nil; } // 调用自定义大头针的方法 -- 获得大头针控件 YXYAnnotationView *annotationView = [YXYAnnotationView annotationViewWithMapView:mapView]; // 传数据(annotationView.annotation是 MKAnnotationView本身具备的属性) annotationView.annotation = annotation; return annotationView;}
MKAnnotationView
地图上的大头针控件是MKAnnotationView
MKAnnotationView的属性
大头针模型 @property (nonatomic, strong) id <MKAnnotation> annotation;
显示的图片 @property (nonatomic, strong) UIImage *image;
是否显示标注 @property (nonatomic) BOOL canShowCallout;
标注的偏移量 @property (nonatomic) CGPoint calloutOffset;
标注右边显示什么控件 @property (strong, nonatomic) UIView *rightCalloutAccessoryView;
标注左边显示什么控件 @property (strong, nonatomic) UIView *leftCalloutAccessoryView;
自定义MKAnnotationView
示例代码:
/** 纬度范围:N 3°51′ ~ N 53°33′ 经度范围:E 73°33′ ~ E 135°05′ */#import "ViewController.h"#import <MapKit/MapKit.h>#import "YXYMKAnnotation.h"// 引入自定义的大头针数据模型(必须自定义,否则敲不出MKAnnotation)#import "YXYAnnotationView.h"@interface ViewController ()<MKMapViewDelegate,MKAnnotation>@property (nonatomic, strong) MKMapView *mapView;@end@implementation ViewController- (MKMapView *)mapView{ if (_mapView == nil) { _mapView = [[MKMapView alloc]initWithFrame:self.view.bounds]; } return _mapView;}- (void)viewDidLoad { [super viewDidLoad]; self.mapView.mapType = MKMapTypeStandard; self.mapView.userTrackingMode = MKUserTrackingModeFollow; self.mapView.delegate = self; [self.view addSubview:self.mapView];// 定位当前位置 self.mapView.showsUserLocation = YES; YXYMKAnnotation *annotation = [[YXYMKAnnotation alloc]init]; annotation.coordinate = CLLocationCoordinate2DMake(39.90989, 116.4); annotation.title = @"天朝"; annotation.subtitle = @"天朝无线娱乐有限公司"; annotation.icon = @"category_2@2x"; [self.mapView addAnnotation:annotation]; YXYMKAnnotation *annotation1 = [[YXYMKAnnotation alloc]init]; annotation1.coordinate = CLLocationCoordinate2DMake(39.90989, 116.46); annotation1.title = @"天朝"; annotation1.subtitle = @"天朝无线娱乐超市"; annotation1.icon = @"category_1@2x"; [self.mapView addAnnotation:annotation1];}
示例代码:YXYAnnotationView.h
文件中
#import <MapKit/MapKit.h>@interface YXYAnnotationView : MKAnnotationView// 自定义大头针样式#pragma mark 创建annotationView+ (instancetype)annotationViewWithMapView:(MKMapView *)mapView;@end
示例代码:YXYAnnotationView.m
文件中
#import "YXYAnnotationView.h"#import "YXYMKAnnotation.h"@interface YXYAnnotationView ()@property (nonatomic, weak) UIImageView *iconView;@end@implementation YXYAnnotationView// 创建annotationView -- 与重用cell方法很像+ (instancetype)annotationViewWithMapView:(MKMapView *)mapView{ static NSString *ID = @"custom";// 标示符 YXYAnnotationView *annotationView = (YXYAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID]; if (annotationView == nil) {// 传入循环利用标识来创建大头针控件 annotationView = [[YXYAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:ID]; } return annotationView;}- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) { // 显示标题和子标题 self.canShowCallout = YES; // 左边显示一个图片 UIImageView *iconView = [[UIImageView alloc]init]; iconView.bounds = CGRectMake(0, 0, 50, 50); self.leftCalloutAccessoryView = iconView; self.iconView = iconView; } return self;}#pragma mark 重写 annotation 的setter方法- (void)setAnnotation:(YXYMKAnnotation *)annotation{ // 初始化父类的三个属性 [super setAnnotation:annotation]; // 设置大头针图标,在父类的三个属性基础上 添加一个图片属性 self.image = [UIImage imageNamed:annotation.icon]; // 给标题中的辅助图设置图片 self.iconView.image = self.image;}@end
MKPinAnnotationView
MKPinAnnotationView是MKAnnotationView的子类
MKPinAnnotationView比MKAnnotationView多了2个属性
大头针颜色 @property (nonatomic) MKPinAnnotationColor pinColor;
大头针第一次显示时是否从天而降 @property (nonatomic) BOOL animatesDrop;
创建系统自带的大头针样式(MKPinAnnotationView):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ // 1.先从缓存池中取出可以循环利用的大头针控件 static NSString *ID = @"anno"; MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID]; // 2.缓存池中没有可以循环利用的大头针控件 if (annotationView == nil) { // 传入循环利用标识来创建大头针控件 annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID]; // 设置头的颜色 annotationView.pinColor = MKPinAnnotationColorPurple; // 从天而降 annotationView.animatesDrop = YES; // 显示标题和子标题 annotationView.canShowCallout = YES; // 标题view的位置偏移量// annotationView.calloutOffset = CGPointMake(0, -10); // annotationView 可以设置左右辅助视图// annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];// annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoDark]; // 往大头针里面添加一个按钮(测试)// [annotationView addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]]; } // 3.传递模型(更新大头针数据,覆盖掉之前的旧数据) annotationView.annotation = annotation; // 4.设置图片 // annotationView.image = [UIImage imageNamed:annotation.icon]; return annotationView;}
- 自定义:地图标注(大头针)
- iOS-地图标注(大头针)
- 百度地图自定义大头针图片和添加标注
- 地图(有界面 自定义大头针)
- 百度地图自定义大头针
- ios-地图-自定义大头针
- iOS移除地图标注大头针
- 【iPhone Demo】地图自定义大头针
- 【iPhone Demo】地图自定义大头针
- iOS 百度地图自定义大头针。
- MKMapView :addAnnotation自定义地图大头针
- 百度地图使用(二)自定义大头针和弹出气泡
- iOS地图和定位服务(自定义大头针)
- 百度地图使用(二)自定义大头针和弹出气泡
- 地图与定位(三)自定义大头针视图
- swift地图定位(十四)使用 MKAnnotationView自定义大头针视图
- ios 地图 自定义标注
- 04 将当前位置用大头针标注到百度地图上
- JavaScript学习笔记8-jQuery基本过滤选择器深度解析
- 黑马程序员-----网络通信
- 我心中的软件测试
- hdoj 2952 Counting Sheep 【dfs】
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
- 自定义:地图标注(大头针)
- poj 1160 Post Office(经典dp)
- 面向接口设计思想
- Quartz()整合Spring容器中bean及动态调度任务
- 建造者模式(二):游戏角色设计的建造者模式解决方案
- zzuli OJ 1097: 计算平均成绩(函数专题)
- LeetCode Factorial Trailing Zeroes
- ListView的高度计算公式
- 1635: [Usaco2007 Jan]Tallest Cow 最高的牛