高德地图自定义气泡

2019-08-02  本文已影响0人  琪琪_729b

自定义一个需要的气泡view:AliMapViewCustomCalloutView,继承自UIView

新建一个继承自MAAnnotationView的类:AliMapViewCustomAnnotationView,需要导入头文件#import<MAMapKit/MAMapKit.h>

在.m文件中引用AliMapViewCustomCalloutView.h 并重写选中方法

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

-(void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    if(self.selected== selected)

    {

        return;

    }

    if(selected)

    {

        if(self.calloutView==nil)

        {

            self.calloutView = [[RRParkCalloutView 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.titleLabel.text = self.annotation.title;

        self.calloutView.contentLabel.text = self.annotation.subtitle;

        [self addSubview:self.calloutView];

        if([self.standardintValue] == 0) {

//设置不同状态下的选中大头针图片

            self.image = [UIImage imageNamed:@"parkFreeClick"];

        }else{

            self.image = [UIImage imageNamed:@"parkChargeClick"];

        }

    }

    else

    {

        if([self.standardintValue] == 0) {

            self.image = [UIImage imageNamed:@"parkFree"];

        }else{

            self.image = [UIImage imageNamed:@"parkCharge"];

        }

        [self.calloutView removeFromSuperview];

    }

    [supersetSelected:selectedanimated:animated];

}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {

    UIView*view = [superhitTest:pointwithEvent:event];

    if(view ==nil) {

        CGPoint tempoint = [self.calloutView.naviButton convertPoint:point fromView:self];

        CGPoint dePoint = [self.calloutView.detailButton convertPoint:point fromView:self];

        if (CGRectContainsPoint(self.calloutView.naviButton.bounds, tempoint))

        {

            view =self.calloutView.naviButton;

//点击导航按钮的响应

            [[NSNotificationCenter defaultCenter] postNotificationName:@"naviPark" object:self.annotation.title];

            [self.calloutView removeFromSuperview];

        }else if(CGRectContainsPoint(self.calloutView.detailButton.bounds, dePoint)){

            view =self.calloutView.detailButton;

//点击查看详情按钮的响应

            [[NSNotificationCenter defaultCenter] postNotificationName:@"detailPark" object:self.annotation.title];

            [self.calloutView removeFromSuperview];

        }else{

            [self.calloutView removeFromSuperview];

        }

    }

    returnview;

}

在controller中调用

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

        AliMapViewCustomAnnotationView *annotationView = (AliMapViewCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

       if(annotationView ==nil)

        {

            annotationView = [[AliMapViewCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];

        }

效果图
上一篇下一篇

猜你喜欢

热点阅读