(一)添加默认样式点标记

  • iOS SDK提供的大头针标注MAPinAnnotationView,通过它可以设置大头针颜色、是否显示动画、是否支持长按后拖拽大头针改变坐标等。
    **
  • **这里用到的类是
    MAPinAnnotationView
    让我们对它的属性有个了解。
    **
  • 继承关系图:

  • 属性图:

iOS SDK提供的大头针标注MAPinAnnotationView,通过它可以设置大头针颜色、是否显示动画、是否支持长按后拖拽大头针改变坐标等。在地图上添加大头针标注的步骤如下:

(1) 修改ViewController.m文件,在viewDidAppear方法中添加如下所示代码添加标注数据对象。

- (void)viewDidAppear:(BOOL)animated
{[super viewDidAppear:animated];MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018);pointAnnotation.title = @"方恒国际";pointAnnotation.subtitle = @"阜通东大街6号";[_mapView addAnnotation:pointAnnotation];
}

(2) 实现 协议中的 mapView:viewForAnnotation:回调函数,设置标注样式。如下所示:


- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{if ([annotation isKindOfClass:[MAPointAnnotation class]]){static NSString *pointReuseIndentifier = @"pointReuseIndentifier";MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];if (annotationView == nil){annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];}annotationView.canShowCallout= YES;       //设置气泡可以弹出,默认为NOannotationView.animatesDrop = YES;        //设置标注动画显示,默认为NOannotationView.draggable = YES;        //设置标注可以拖动,默认为NOannotationView.pinColor = MAPinAnnotationColorPurple;return annotationView;}return nil;
}

(二)自定义标注图标

  • 若大头针样式的标注不能满足您的需求,您可以自定义标注图标
    其实就是修改一下MAAnnotationView 的 image 属性
    步骤:

**(1) 添加标注数据对象,可参考大头针标注的步骤(1)。
(2) 导入标记图片文件到工程中。这里我们导入一个名为 restauant.png 的图片文件。
(3) 在 协议的回调函数mapView:viewForAnnotation:中修改 MAAnnotationView 对应的标注图片。示例代码如下:**

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{if ([annotation isKindOfClass:[MAPointAnnotation class]]){static NSString *reuseIndetifier = @"annotationReuseIndetifier";MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];if (annotationView == nil){annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];}annotationView.image = [UIImage imageNamed:@"restaurant"];//设置中心点偏移,使得标注底部中间点成为经纬度对应点annotationView.centerOffset = CGPointMake(0, -18);return annotationView;}return nil;
}

(三)添加自定义气泡

  • 气泡在iOS中又称为callout,它由背景和气泡内容构成,如下图所示:

思路:
我们点击标注可以弹出气泡
是利用了MAAnnotationView 的这个方法


- (void) setSelected:animated:

我们需要重写这个方法,选中时新建并添加气泡视图,传入数据;非选中时删除气泡视图。
**
1.自定义一个气泡视图
2.定义MAAnnotationView 的子类,重写 setSelected:animated: 方法
3.修改ViewController.m,在MAMapViewDelegate的回调方法mapView:viewForAnnotation中的修改annotationView的类型
**

1.每个气泡显示的内容是根据您的需求定义的,这里我们按照如上图所示的气泡介绍实现一个自定义气泡的步骤:

(1) 新建自定义气泡类 CustomCalloutView,继承 UIView。

(2) 在 CustomCalloutView.h 中定义数据属性,包含:图片、商户名和商户地址。

@interface CustomCalloutView : UIView@property (nonatomic, strong) UIImage *image; //商户图
@property (nonatomic, copy) NSString *title; //商户名
@property (nonatomic, copy) NSString *subtitle; //地址@end

(3) 在CustomCalloutView.m中重写UIView的drawRect方法,绘制弹出气泡的背景。

#define kArrorHeight        10- (void)drawRect:(CGRect)rect
{[self drawInContext:UIGraphicsGetCurrentContext()];self.layer.shadowColor = [[UIColor blackColor] CGColor];self.layer.shadowOpacity = 1.0;self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);}- (void)drawInContext:(CGContextRef)context
{CGContextSetLineWidth(context, 2.0);CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);[self getDrawPath:context];CGContextFillPath(context);}- (void)getDrawPath:(CGContextRef)context
{CGRect rrect = self.bounds;CGFloat radius = 6.0;CGFloat minx = CGRectGetMinX(rrect),midx = CGRectGetMidX(rrect),maxx = CGRectGetMaxX(rrect);CGFloat miny = CGRectGetMinY(rrect),maxy = CGRectGetMaxY(rrect)-kArrorHeight;CGContextMoveToPoint(context, midx+kArrorHeight, maxy);CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);CGContextClosePath(context);
}

(4) 定义用于显示气泡内容的控件,并添加到SubView中。

如上图所示气泡,我们需要一个UIImageView和两个UILabel,添加方法如下:

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50#define kTitleWidth         120
#define kTitleHeight        20@interface CustomCalloutView ()@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;@end@implementation CustomCalloutView- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self){self.backgroundColor = [UIColor clearColor];[self initSubViews];}return self;
}- (void)initSubViews
{// 添加图片,即商户图self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];self.portraitView.backgroundColor = [UIColor blackColor];[self addSubview:self.portraitView];// 添加标题,即商户名self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];self.titleLabel.font = [UIFont boldSystemFontOfSize:14];self.titleLabel.textColor = [UIColor whiteColor];self.titleLabel.text = @"titletitletitletitle";[self addSubview:self.titleLabel];// 添加副标题,即商户地址self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];self.subtitleLabel.font = [UIFont systemFontOfSize:12];self.subtitleLabel.textColor = [UIColor lightGrayColor];self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";[self addSubview:self.subtitleLabel];
}
(5) 在CustomCalloutView.m中给控件传入数据。- (void)setTitle:(NSString *)title
{self.titleLabel.text = title;
}- (void)setSubtitle:(NSString *)subtitle
{self.subtitleLabel.text = subtitle;
}- (void)setImage:(UIImage *)image
{self.portraitView.image = image;
}

2.自定义AnnotationView,步骤如下:

(1) 新建类CustomAnnotationView,继承MAAnnotationView或MAPinAnnotationView。若继承MAAnnotationView,则需要设置标注图标;若继承MAPinAnnotationView,使用默认的大头针标注

(2) 在CustomAnnotationView.h中定义自定义气泡属性,代码如下所示:

#import "CustomCalloutView.h"@interface CustomAnnotationView : MAAnnotationView@property (nonatomic, readonly) CustomCalloutView *calloutView;@end

(3) 在CustomAnnotationView.m中修改calloutView属性,如下:

@interface CustomAnnotationView ()@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;@end

(4) 重写选中方法setSelected。选中时新建并添加calloutView,传入数据;非选中时删除calloutView。

#define kCalloutWidth       200.0
#define kCalloutHeight      70.0- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{if (self.selected == selected){return;}if (selected){if (self.calloutView == nil){self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);}self.calloutView.image = [UIImage imageNamed:@"building"];self.calloutView.title = self.annotation.title;self.calloutView.subtitle = self.annotation.subtitle;[self addSubview:self.calloutView];}else{[self.calloutView removeFromSuperview];}[super setSelected:selected animated:animated];
}

注意:提前导入building.png图片。
**
3.修改ViewController.m
**
在MAMapViewDelegate的回调方法mapView:viewForAnnotation中的修改annotationView的类型,代码如下:

#import “CustomAnnotationView.h”- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{if ([annotation isKindOfClass:[MAPointAnnotation class]]){static NSString *reuseIndetifier = @"annotationReuseIndetifier";CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];if (annotationView == nil){annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];}annotationView.image = [UIImage imageNamed:@"restaurant"];// 设置为NO,用以调用自定义的calloutViewannotationView.canShowCallout = NO;// 设置中心点偏移,使得标注底部中间点成为经纬度对应点annotationView.centerOffset = CGPointMake(0, -18);return annotationView;}return nil;
}

iOS 高德地图(五)绘制点标记相关推荐

  1. android高德地图改变绘制点标记位置,绘制点标记-在地图上绘制-开发指南-Android 室内地图SDK | 高德地图API...

    添加标记点 1)函数体: int addMarker(Marker marker); 2)参数: marker– 地图上添加自定义图标对象并显示 3)说明: Marker对象的构造方式见参考手册及de ...

  2. 高德地图自定义绘制点标记

    private void setCompanyLocation() {LatLng latLng = new LatLng(21.2283990000, 110.4032290000);MarkerO ...

  3. flutter引入高德地图_Flutter笔记-调用原生IOS高德地图sdk

    一.前言 2017年底因公司业务组合部门调整,新的团队部分维护的项目用React Native技术混合开发.为适应环境变化,开启疯狂RN学习之旅,晚上回来啃资料看视频.可能由于本身对RN技术体验不感冒 ...

  4. vue使用高德地图并且绘制多边形

    使用鼠标工具在高德地图上绘制多边形时需要使用到一个插件,如果直接new出该工具会提示该工具不是一个函数之类的错误,在使用之前需要先告诉vue,你所是使用的是一个插件 AMap.plugin([&quo ...

  5. JavaScript高德地图中绘制echarts图表随地图移动

    JavaScript高德地图中绘制echarts图表随地图移动** 先上效果图 实现方法如下: 使用高德地图"信息窗体",信息窗体AMap.InfoWindow的属性content ...

  6. 高德地图五:室内地图功能

    高德地图五:室内地图功能 什么是室内地图 室内地图一般指大型室内建筑的内部地图,与室外地图相比,更注重小区域.大比例尺.高精度和精细化的内部元素展现. 室内地图涉及商业设施.交通设施.文化设施.教育设 ...

  7. 高德地图限制绘制区域

    高德地图在绘制过程中没有提供监听过程的Api:制造错误来进行中断绘制 // 创建地图createMap() {let self = this;self.center = [self.gpsAll[0] ...

  8. 高德地图web绘制省、市、区 边界线和面积图

    高德地图web绘制省.市.区 边界线和面积图 高德地图官方API 图例 总结要点 申请高德地图的key 用于后边web调用 高德地图key申请 引用 <script type="tex ...

  9. 高德地图上绘制城市名字和带涟漪的点标记

    接上一篇"vue中基于echarts和基于高德地图的两种地图下钻与上浮方式" import noEmerIcon from "./img/emerManagement/n ...

  10. iOS 高德地图开发详解

    Demo地址 如果有所帮助记得关注,点Star demo中添加了查看路况功能,如果不需要,可以删除. ##一:基本地图功能实现 ####1.申请密钥流程 申请密钥链接 2.配置环境(重点) 高德地图提 ...

最新文章

  1. 父类引用指向子类对象
  2. mysql smack_super-smack测试mysql性能
  3. 2013年,我跟哥们都是大厂Java工程师!后来,他转行了!现在,他的收入是我的5倍!...
  4. 如何修改xd.properties文件中对象存储文件信息_对块存储、文件存储、对象存储的认识总结...
  5. 怎么制作铁闸门_“短笛”拿铁,最近的心头好!
  6. SSM框架搭建(四) springmvc和mybatis的配置
  7. 网络和新媒体能申请计算机硕士吗,网络与新媒体专业可以报考公务员吗
  8. shell编程-条件判断与流程控制
  9. Android获得全局进程信息以及进程使用的内存情况
  10. oracle数据库中汉字转化成拼音
  11. [附源码]Python计算机毕业设计Django大学生考勤管理系统论文
  12. 手机html5翻页效果代码,jquery html5手机端翻书效果_手指滑动书本翻页效果代码
  13. Windows 10 升级软件 Windows 10 易升
  14. ESP8266串口wifi模块 NodeMCU Lua V3物联网开发板 CH340上传程序不显示
  15. dataframe去掉行索引_DataFrame按索引删除行、列
  16. 点对点加密文件传输工具Filegogo
  17. 难道是C3p0的问题
  18. 福布斯:雅虎代理权争夺战背后的十大问题
  19. 迅为3399开发板Android系统-使用strace跟踪系统调用
  20. 希尔排序----附图解(C语言)

热门文章

  1. 微信CRM系统哪家好?
  2. 用Python海龟画图画哆啦A梦
  3. Android Alarm详解
  4. android 浏览器横屏,2013安卓平台浏览器横屏
  5. VTK笔记-切面重建-使用交互器更新断层图的奇异现象的问题排查
  6. 阻容感基础10:电感器分类(2)-功率电感器
  7. js 数组扁平化和树之间相互转换
  8. ASM+LINUX+ORACLE_11G安装
  9. [AHK]在当前目录中运行DOS命令行--DosHere
  10. 爬虫之requests