加入收藏 | 设为首页 | 会员中心 | 我要投稿 无忧刷机网 - 51刷机网 (https://www.51shuaji.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 苹果频道 > 苹果资讯 > 正文

iOS高德地图之自定义大头针and泡泡view

发布时间:2017-06-21 23:33:32 所属栏目:苹果资讯 来源:www.jianshu.com
导读:啥都不说先看效果图demoIMG_0270.PNG先来说说如何自定义大头针以及点击大头针时弹出的泡泡view一:自定义大头针新建CustomAnnotationView继承自MAAnnotationView添加属性重写-(id)initWithAnnotation:(idlt;MAAnnotationgt;)annotationreuseIdentifier:(NSStri

啥都不说先看效果图demoIMG_0270.PNG先来说说如何自定义大头针以及点击大头针时弹出的泡泡view一:自定义大头针新建CustomAnnotationView继承自MAAnnotationView添加属性重写-(id)initWithAnnotation:(id<MAAnnotation>)annotationreuseIdentifier:(NSStrin...

啥都不说先看效果图demo

iOS高德地图之自定义大头针and泡泡view
IMG_0270.PNG

先来说说如何自定义大头针以及点击大头针时弹出的泡泡view

一 : 自定义大头针
  • 新建CustomAnnotationView 继承自MAAnnotationView
  • 添加属性
  • 重写- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
  • 重写- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 解决泡泡view超出父控件事件响应问题
  • 重写- (void)setSelected:(BOOL)selected animated:(BOOL)animated
二 : 自定义泡泡View
  • 新建自定义气泡类 CustomCalloutView,继承 UIView
  • 在 CustomCalloutView.h 中定义数据属性,包含:图片、商户名和商户地址。(随便你怎么搞,在这里我就搞了一个xib)
iOS高德地图之自定义大头针and泡泡view
Snip20170620_1.png
  • 在上面新建的CustomAnnotationView.h中定义自定义气泡属性
#import "CustomCalloutView.h"  @interface CustomAnnotationView : MAAnnotationView  @property (nonatomic, readonly) CustomCalloutView *calloutView;  @end
  • 重写选中方法- (void)setSelected:(BOOL)selected animated:(BOOL)animated。选中时新建并添加calloutView,传入数据;非选中时删除calloutView。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {     if (self.selected == selected)     {         return;     }      if (selected)     {         if (self.calloutView == nil)         {             /* Construct custom callout. */              self.calloutView = [CustomCalloutView calloutView];             self.calloutView.frame = 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 addSubview:self.calloutView];     }     else     {         [self.calloutView removeFromSuperview];     }      [super setSelected:selected animated:animated]; }
  • 修改ViewController.m,在MAMapViewDelegate的回调方法mapView:viewForAnnotation中的修改annotationView的类型
#pragma mark - MAMapViewDelegate  - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {     if ([annotation isKindOfClass:[MAPointAnnotation class]])     {         static NSString *customReuseIndetifier = @"customReuseIndetifier";          CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];          if (annotationView == nil)         {             annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];             // must set to NO, so we can show the custom callout view.             annotationView.canShowCallout = NO;             annotationView.draggable = YES;             annotationView.calloutOffset = CGPointMake(0, -5);         }          return annotationView;     }      return nil; }
  • 用于调整泡泡view显示不全问题
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {     /* Adjust the map center in order to show the callout view completely. */     if ([view isKindOfClass:[CustomAnnotationView class]]) {         CustomAnnotationView *cusView = (CustomAnnotationView *)view;         CGRect frame = [cusView convertRect:cusView.calloutView.frame toView:self.mapView];          frame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin));          if (!CGRectContainsRect(self.mapView.frame, frame))         {             /* Calculate the offset to make the callout view show up. */             CGSize offset = [self offsetToContainRect:frame inRect:self.mapView.frame];              CGPoint theCenter = self.mapView.center;             theCenter = CGPointMake(theCenter.x - offset.width, theCenter.y - offset.height);              CLLocationCoordinate2D coordinate = [self.mapView convertPoint:theCenter toCoordinateFromView:self.mapView];              [self.mapView setCenterCoordinate:coordinate animated:YES];         }      } }

(编辑:无忧刷机网 - 51刷机网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读