美文网首页地图相关
百度地图自定义吹出框

百度地图自定义吹出框

作者: CS二哥 | 来源:发表于2015-12-01 14:15 被阅读818次

直入正题吧!

这些都是知道的了,看文档添加就行了!

实现三个代理方法:

这个方法类似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;

}

}

}

相关文章

  • 百度地图自定义吹出框

    直入正题吧! 实现三个代理方法: 这个方法类似tableview添加cell,都是创建annotation #pr...

  • 高德 ios 自定义气泡添加点击事件无效问题

    在使用高德地图sdk开发的时候,需要自定义气泡吹出框,发现气泡添加的点击事件或者button都没响应 原因:自定义...

  • iOS-高德自定义气泡添加点击事件无效问题

    问题 在使用高德地图sdk开发的时候,需要自定义气泡吹出框,发现气泡添加的点击事件或者button都没响应 原因 ...

  • 地图| 百度地图源码级使用大全

    本文基于一个百度地图上的需求实现,记录下百度地图使用中的点滴,后续会持续更新。 地图上自定义可点击的展示框 需求:...

  • 1.百度地图bug

    1.百度地图接口蓝屏 百度地图接口蓝屏————自定义地图大小不能使用标签名百度地图经纬度数值过大:百度1.0表示的...

  • 解决百度地图双击放大手势与单击手势冲突的问题

    百度地图双击会放大地图,但是当我单击百度地图时,需要隐藏我自定义的一些视图,但是当我添加单击手势后,百度地图的双击...

  • iOS 百度地图的高级用法

    本文主要介绍百度地图的自定义用法,比如创建地图、添加标注点、添加路径轨迹等问题不在本文讨论范围,请点击百度地图开放...

  • 集成百度地图,看看就好

    集成百度地图,实现基本定位,检索,自定义大头针。 1.集成百度地图,当然前提还是去注册百度地图开发者 2.接下来创...

  • 百度地图自定义气泡

    iOS-百度地图自定义气泡IOS百度地图自定义大头针和气泡 重点是定义了一个全局变量来取气泡的数据模型:stati...

  • 地图打点

    实现效果如下: 地图打点主要常用的地图分两类: 百度地图 高德地图 高德地图 高德地图可以使用自定义内容标记,通过...

网友评论

  • 翀鹰精灵:麻烦分享下demo 谢谢 2807338860@qq.com
  • 小贤哥:楼主麻烦分享下demo,谢谢 421518143@qq.com
  • 幽默魔:麻烦看下demo630535161@qq.com
    CS二哥:@幽默魔 已发
  • 程序员也有梦:请问有demo么
    CS二哥:@程序员也有梦 已发
    程序员也有梦:@CS二哥 gzzhor@163.com,我在做这个自定义大头针和自定义泡泡,希望能看看您的demo研究下
    CS二哥:@程序员也有梦 有的,可能不太完善,留下邮箱发给你自己研究一下吧

本文标题:百度地图自定义吹出框

本文链接:https://www.haomeiwen.com/subject/qdmwcttx.html