百度地图自定义吹出框
2019独角兽企业重金招聘Python工程师标准>>>
百度地图自定义吹出框
直入正题吧!
这些都是知道的了,看文档添加就行了!
实现三个代理方法:
这个方法类似tableview添加cell,都是创建annotation
#pragma mark #pragma mark - BMKMapview delegate -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation;
这个方法在点击地图marker时所触发(并显示callout)
-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view;
这个方法在点击地图任意位置,相当于隐藏callout
-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view;
原理:地图上的marker是在viewForAnnoation里创建的,同时也会 隐含的为我们创建一个CalloutView,就是自带的吹出框,只是我们看不到源码。其实这个吹出框(CalloutView)也是一个 annotation,也会在viewForAnnotation里被创建,他的坐标应该和这个点的marker坐标一样,只要明白了这一点,就行了,marker和吹出框是两个不同的annotation,他们有同样的coordinate。
第一步:
自定义一个Annotation,为了简单方便,我就直接继承了mapview自带的BMKPointAnnotation,这是一个经典的图钉marker。
以下部分是为自定义annotation添加显示更多信息
第二步:
创建一个(自定义的CalloutView)的Annotation,相当于显示calloutView的annotation。
[注意] 继承自NSObject<BMKAnnotation>
第三步:
这一步我们创建自定义的View,想要什么布局就写什么样的布局,想要多少属性就加多少属性。
[注意:继承自BMKAnnotationView]
AnnotationPointCell是ContentView里的subview,这个view就是显示各个组件,并赋不同的值
CallOutAnnotationView.m
#import "CallOutAnnotationView.h"
#define Arror_height 6
@implementation CallOutAnnotationView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.canShowCallout = NO;
self.centerOffset = CGPointMake(0, -55);
self.frame = CGRectMake(0, 0, 240, 80);
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width-15, self.frame.size.height-15)];
contentView.backgroundColor = [UIColor clearColor];
[self addSubview:contentView];
self.contentView = contentView;
} return self;
}
-(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:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0].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),
// midy = CGRectGetMidY(rrect),
maxy = CGRectGetMaxY(rrect)-Arror_height;
CGContextMoveToPoint(context, midx+Arror_height, maxy);
CGContextAddLineToPoint(context,midx, maxy+Arror_height);
CGContextAddLineToPoint(context,midx-Arror_height, 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);
}
AnnotationPointCell里面想添加什么内容就自己定吧
第四步:
下面是VC里面的主要代码
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
{
static NSString * annotationIndentifier = @"customAnnotation";
if ([annotation isKindOfClass:[CusTomPointAnnotation class]]) {
BMKPinAnnotationView * annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIndentifier];
/**
设置大头针图片
annotationView.image = [UIImage imageNamed:@""];
**/
[annotationView setAnimatesDrop:YES];
//[注意]在添加marker的判断里一定要设置markerannotation.canShowCallout =NO; 否则点击marker会默认显示系统的吹出框
annotationView.canShowCallout = NO;
return annotationView;
}else if ([annotation isKindOfClass:[CalloutMapAnnotation class]]){
//此时annotation就是我们calloutview的annotation
CalloutMapAnnotation *ann = (CalloutMapAnnotation*)annotation;
//如果可以重用
CallOutAnnotationView *calloutannotationview = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"calloutview"];
//否则创建新的calloutView
if (!calloutannotationview) {
calloutannotationview = [[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"calloutview"];
AnnotationPointCell *cell = [[AnnotationPointCell alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[calloutannotationview.contentView addSubview:cell];
calloutannotationview.busInfoView = cell;
}
//开始设置添加marker时的赋值
calloutannotationview.busInfoView.nameLabel.text = [ann.locationInfo objectForKey:@"alias"];
calloutannotationview.busInfoView.contentLabel.text = [ann.locationInfo objectForKey:@"speed"];
calloutannotationview.busInfoView.timeLabel.text =[ann.locationInfo objectForKey:@"degree"];
// calloutannotationview.busInfoView.titleImageView.image = [UIImage imageNamed:@""];
return calloutannotationview;
}
return nil;
}
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
//CustomPointAnnotation 是自定义的marker标注点,通过这个来得到添加marker时设置的pointCalloutInfo属性
CusTomPointAnnotation *annn = (CusTomPointAnnotation*)view.annotation;
if ([view.annotation isKindOfClass:[CusTomPointAnnotation class]]) {
//如果点到了这个marker点,什么也不做
if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude && _calloutMapAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
return;
} //如果当前显示着calloutview,又触发了select方法,删除这个calloutview annotation(处理多个气泡的情况)
if (_calloutMapAnnotation) {
[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation=nil;
} //创建搭载自定义calloutview的annotation
_calloutMapAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];
//把通过marker(ZNBCPointAnnotation)设置的pointCalloutInfo信息赋值给CalloutMapAnnotation
_calloutMapAnnotation.locationInfo = annn.pointCalloutInfo;
[mapView addAnnotation:_calloutMapAnnotation];
[mapView setCenterCoordinate:view.annotation.coordinate animated:YES];
}else if ([view isKindOfClass:[CallOutAnnotationView class]]) {
//这里处理点击自定义吹出框的事件
}
}
#pragma mark - 这个方法在点击地图任意位置,相当于隐藏callout
//这个方法在点击地图任意位置,相当于隐藏callout
-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{
if (_calloutMapAnnotation&&![view isKindOfClass:[CallOutAnnotationView class]]&&![view isKindOfClass:[CalloutMapAnnotation class]]) {
if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude) {
[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation = nil;
}
}
}
转载于:https://my.oschina.net/fadoudou/blog/732866
百度地图自定义吹出框相关推荐
- android百度地图 自定义气泡,百度地图自定义吹出框(气泡)(转)
demo模式:非ARC,使用storyboard. demo资源: http://download.csdn.net/detail/mad1989/5252037 Step1 创建demo,并添加百度 ...
- android中使用百度地图绘制弹出框的覆盖物
这几天在项目中引入了百度地图,实现的功能就是类似美团的地图查看周边团购那样的功能,实现的弹出框布局要比美团复杂一些. 下面直接上代码: 通过这个方法在指定的坐标创建一个覆盖物 mBaiduMap.se ...
- android 百度地图覆盖物popupwindow自动弹出,android实现百度地图自定义弹出窗口功能...
public class MyPopupOverlay extends ItemizedOverlay { private Context context = null; // 这是弹出窗口, 包括内 ...
- ios 一步一步学会自定义地图吹出框(CalloutView)--(百度地图,高德地图,google地图)
前言 在ios上边使用地图库的同学肯定遇到过这样的问题:吹出框只能设置title和subtitle和左右的view,不管是百度地图还是高德地图还是自带的google地图,只提供了这四个属性,如果想添加 ...
- OpenLayers标记地图点及点击地图点显示自定义弹出框
css代码(设置弹出框样式) /*设置弹出框样式*/.ol-popup {position: absolute;background-color: #eeeeee;-webkit-filter: dr ...
- 百度地图 自定义结果面板+分页+图层标注(标注点+搜索)
百度地图 自定义结果面板+分页+图层标注(标注点+搜索) 示例一: <html> <head><meta http-equiv="Content-Type&qu ...
- 百度地图自定义本地图标无法显示
目录 1.问题 2.代码 解决方式 参考博文 1.问题 百度地图自定义图标,用本地的图标发现无法显示,百度搜索后又说用require变成模块的,使用之后出现新的问题,Uncaught Referenc ...
- react native仿微信性别选择-自定义弹出框
简述 要实现微信性别选择需要使用两部分的技术: 第一.是自定义弹出框: 第二.单选框控件使用: 效果 实现 一.配置弹出框 弹出框用的是:react-native-popup-dialog(Git地址 ...
- android自定义弹出框样式实现
前言: 做项目时,感觉Android自带的弹出框样式比较丑,很多应用都是自己做的弹出框,这里也试着自己做了一个. 废话不说先上图片: 实现机制 1.先自定义一个弹出框的样式 2.自己实现CustomD ...
最新文章
- php 模块指令,php artisan module常用命令
- 路由器下一跳地址怎么判断_网络基本功三:细说路由器
- lintcode:合并排序数组
- tensorflow2.0 图像处理项目_航天泰坦丨国产自主遥感图像处理软件当自强
- Matlab系列教程_基础知识_绘图(二)
- 【语音加密】基于matlab GUI语音信号加密解密【含Matlab源码 295期】
- 《码出高效 Java开发手册》书籍源码及相关代码示例
- ERP学习 之 财务管理
- 客流量统计分析系统应用解决方案
- gridview隐藏列的方法
- 腾讯地图和百度地图经纬度的相互转换
- 出口商贸易融资工具:汇出汇款融资
- IBM Websphere MQ 基础3:Listener监听器
- 百度UNIT 机器人多轮对话技能创建以及API调用
- APP功能测试点(全)
- python pandas合并单元格_利用Python pandas对Excel进行合并的方法示例
- Elasticsearch - 压测方案之 esrally 简介
- C语言---char *与const char *
- 【2017-02-05】【抓包】鼠大侠鼠标连点器去广告
- 20个JS精简代码无形装逼集合,最为致命,记得收藏好